In Vim, what is the “alternate file”?

前端 未结 4 668
慢半拍i
慢半拍i 2021-01-01 09:26

I just ran :help registers in Vim and noticed that # \'contains the name of the alternate file\'.

I have seen an example for renaming files

相关标签:
4条回答
  • 2021-01-01 09:30

    I use it in the buffer context to return to the last buffer that I was editing

    vim foo bar 
    :n 
    :e#
    

    will take you back to foo in that case

    0 讨论(0)
  • 2021-01-01 09:33

    I 've always interpreted the "alternate file" as being the "previous file", so it is an handy way to jump back to the buffer you were editing.

    0 讨论(0)
  • Very useful for...

    Pasting in the name of a file I've just been looking at into the current file.

    You can use <C-R># for this in insert mode or "#p in normal mode.

    Not that useful for...

    Jumping back and forth between two files. It does the job very well, but this is just something I don't generally need to do.

    Even in the example given, I'd probably use:saveas bar.txt instead.

    An Example:

    Say if you're doing a bit of C programming and want to call some function. You can't remember the name of the function, so you place a mark on your current location mA and jump into several different files using tags or grep to find out where the function is declared and what it's actually called.

    Ah - found it. You can copy the name and return to the mark yiw'A

    Uh-oh - we also need to #include the file! Easy - just use the alternate file name register to paste the file name in... Gi#include"<C-R>#"

    Be pleased that you've avoided the distraction of having to go back to the function's declaration and copy out the file name via :let @"=@% or something similar.

    What I'd rather do when jumping between files:

    • When editing two files, it's probably easier to split them, so you can keep both on screen at the same time. If I'm editing 2 files I'll usually be comparing them in some way.
    • Usually I'm interested in 1-3 files (any more and I get confused). I'll often jump into or directly open many other files. Marking the interesting files, or traversing the jump list is usually the way to get around in this case.
    • If you're editing C/C++ where you're switching between a file and it's header, use a plugin! It will be much more convenient.
    0 讨论(0)
  • 2021-01-01 09:49

    The alternate file is the file that was last edited in the current window. Actually when you use some command to open a new buffer, if the buffer that was displayed had a filename associated with it, that filename is recorded as alternate file name.

    See :help alternate-file.

    0 讨论(0)
提交回复
热议问题