42

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 that goes like this:

" Save the current file, foo.txt, as bar.txt
:w bar.txt
" Start editing bar.txt
:e#

So apparently in that case, the file you just saved out is the "alternate file."

Can someone give me a more general definition for the "alternate file" and what else you might use it for?

Nathan Long
  • 122,748
  • 97
  • 336
  • 451
  • 1
    I tend to edit multiple files in tabs - maybe alternate file is more often used with buffers? – Nathan Long Mar 03 '11 at 15:32
  • Ooops - I meant to ask this on Superuser. Is it still possible to migrate questions from here to there? – Nathan Long Mar 03 '11 at 16:06
  • 2
    It can still be migrated, but it isn't off topic here. From the stackoverflow.com FAQ: "if your question generally covers...software tools commonly used by programmers...then you’re in the right place to ask your question!" – DrAl Mar 03 '11 at 16:11

4 Answers4

36

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.

Benoit
  • 76,634
  • 23
  • 210
  • 236
22

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.
Community
  • 1
  • 1
PDug
  • 1,222
  • 8
  • 9
  • 2
    " Native way to switch between C++ source and header `nnoremap :e %:p:s,.h$,.X123X,:s,.cpp$,.h,:s,.X123X$,.cpp,` – Hope Mar 05 '19 at 14:47
  • 1
    Also, if you don't have your vimrc handy or you don't work with C enough to have a mapping, you can use `:e %:r.h` and `%:r.c` or `:e %:r.cpp` to switch reasonably conveniently with no configuration required. – Soren Bjornstad Apr 09 '19 at 00:47
8

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

Rob Parker
  • 503
  • 4
  • 10
5

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.

Xavier T.
  • 40,509
  • 10
  • 68
  • 97