The alternate file is the file you were previously editing in the current window. Each window has its own independent alternate file. Type
:h usr_07.txt, and look for the pattern
alternate. You should find this paragraph:
The file you were previously editing is called the "alternate" file. When you
just started Vim CTRL-^ will not work, since there isn't a previous file.
You can toggle between the current and alternate files by typing on the command line:
... or shorter:
Or, from normal mode, you can press
<C-^> (could be the same as
Suppose you have 3 files A, B, and C. And you launch Vim to edit A:
A is your current file, and there's no alternate file.
If you load B (
:edit B), then A becomes the alternate file, and B becomes the current file. You can toggle between A and B as many times as you want by repeating
If at some point you stop toggling, while your current buffer is B, your alternate file is A, and you ask Vim to edit C (
:edit C), then the current file will become C, and the alternate file will become B.
<C-^> won't make you cycle between A and B anymore, but between B and C.
The name of the alternate file is stored inside the
# register, so you can read it by typing:
You can also see it when you type
:buffers. These commands list all the buffers which are loaded in your current session. Inside the listing, the alternate buffer is marked with the
# sign, while the current buffer is marked with the
aA flags are inside
'cpo', then, whenever you execute a command such as
:read some_file or
some_file will become the alternate file for the current window.
To understand what's the effect of the
aA flags inside
'cpo', you could try the following experiment.
Create 2 files inside your
/tmp folder, named
foo2.txt. The first one containing the word
hello, the second one containing
cd /tmp && echo hello >> foo1.txt && echo world >> foo2.txt
foo1.txt with Vim:
And make sure the
a flag is not inside the
'cpo' option, by typing:
Try to load the alternate file for the current window with
The Ex command should have no effect, and the normal command should raise the error:
E23: No alternate file
In both cases, the buffer displayed in the current window should still be
This result was expected, because there can't be any alternate file for this window, since you've just started Vim.
Now, import the contents of the
Your buffer should now contain:
Again, try to load the alternate file with
<C-^>. The result is still the same.
:read command didn't change / set the alternate file of your current window, because the
a flag wasn't inside
a flag to
'cpo', and re-import the contents of
Your buffer should now contain:
Finally, try to load the alternate file:
/tmp/foo2.txt should be displayed in the current window, because the
a flag is inside
'cpo', and so the argument of the previous
/tmp/foo2.txt, was set as the alternate file of your window.
The same thing happens with the
:write command, and the
You can use the
:write command to write a range of lines of the current buffer inside another file. For example, you could visually select a paragraph, then type:
This should write the visual selection inside
/tmp/foo3.txt. But if the
A flag is not inside
'cpo', then the alternate file of your current window won't be changed. If
A is inside
/tmp/foo3.txt will become the alternate file of your current window.