3

From the docs at http://vimdoc.sourceforge.net/htmldoc/options.html#'cpoptions':

    a   When included, a ":read" command with a file name
        argument will set the alternate file name for the
        current window.

    A   When included, a ":write" command with a file name
        argument will set the alternate file name for the
        current window.

I'm confused by what an "alternate file name" is. And if these two options are unset with cpo-=aA, how will VIM behave differently?

7

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:

:buffer #

... or shorter:

:b#

Or, from normal mode, you can press <C-^> (could be the same as <C-6>).


Suppose you have 3 files A, B, and C. And you launch Vim to edit A:

vim 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 :b# or <C-^>.

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.

From then, :b# and <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:

:echo @#

You can also see it when you type :ls or :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 % sign.


If the aA flags are inside 'cpo', then, whenever you execute a command such as :read some_file or :write some_file, 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 foo1.txt and foo2.txt. The first one containing the word hello, the second one containing world:

cd /tmp && echo hello >> foo1.txt && echo world >> foo2.txt

Open foo1.txt with Vim:

vim foo1.txt

And make sure the a flag is not inside the 'cpo' option, by typing:

:set cpo-=a

Try to load the alternate file for the current window with :b# or <C-^>.

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 foo1.txt.

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 foo2.txt file:

:read foo2.txt

Your buffer should now contain:

hello
world

Again, try to load the alternate file with :b# or <C-^>. The result is still the same.
The :read command didn't change / set the alternate file of your current window, because the a flag wasn't inside 'cpo'.

Add the a flag to 'cpo', and re-import the contents of foo2.txt:

:set cpo+=a
:r foo2.txt

Your buffer should now contain:

hello
world
world

Finally, try to load the alternate file: :b# or <C-^>.

This time, /tmp/foo2.txt should be displayed in the current window, because the a flag is inside 'cpo', and so the argument of the previous :read command, /tmp/foo2.txt, was set as the alternate file of your window.


The same thing happens with the :write command, and the A flag.

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:

:'<,'>write /tmp/foo3.txt

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 'cpo', then /tmp/foo3.txt will become the alternate file of your current window.

2
  • Cool, thanks so much. What practical use does this have?
    – StevieD
    Feb 13 '17 at 15:22
  • 1
    @StevieD The alternate file is useful when you have to frequently switch between 2 files, but don't want to split the current window. As for the a and A flags, I suppose they are useful when you find that some :read or :write commands interfere in your work by setting an alternate file C while you were switching between 2 files A and B. If this happens to you a lot, I guess it would make sense to remove aA from 'cpo', otherwise you could let them inside. Feb 13 '17 at 15:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.