13

Vim keeps a history of ex commands (accessible via : and q:), and that history persists after quitting.

If I run multiple instances of Vim (same user, same home directory), how does Vim arbitrate between the command histories? It seems that the last process to exit wins. Is there a way to keep the command histories of all instances?

  • Really? This history does not persist after quitting for me. (Vim 7.4.52, Ubuntu 14.04) – Doorknob Feb 4 '15 at 21:45
  • 2
    @Doorknob Make sure your ~/.viminfo is owned by you. A common mistake is for people to run sudo vim ... which, if you haven't run vim as your own user before, will create ~/.viminfo as root. If that is the problem, I'd suggest learning to use sudoedit or sudo -e to edit privileged files. – jamessan Feb 4 '15 at 22:04
  • @jamessan Huh, it appears that was indeed the problem; chown'ing my .viminfo back to myself fixed it. (Not sure how its ownership got changed to root, since I've always used sudoedit, but oh well.) – Doorknob Feb 4 '15 at 22:08
8

The answer to your title question is what you observed. From the vim user manual :help 21.3

When you run Vim multiple times, the last one exiting will store its information. This may cause information that previously exiting Vims stored to be lost. Each item can be remembered only once.

However, the filename of the viminfo file (where the command history is stored, among other things like global marks and register contents, if so configured) is changeable! This means that you can setup different 'histories' for different projects or instances of vim. Assuming you don't run more than one vim instance per project, managing the viminfo filename via vimrc (or other project settings plugin) is a great way to handle this.

Setup alternative viminfo files

For project level management, we want to configure vim to save your viminfo to a different file. This can be done within a running vim prior to quitting, or by your vimrc, for example you might have lines in your vimrc that detect a particular directory as belonging to a project.

:set viminfo+=nPath/to/custom/viminfofile

An example of having vimrc automatically set this based on directory:

 if getcwd() == "/projects/projA"
     set viminfo+=n~/.viminfo-projA
 endif

The result of the above is that the project-specific history will be loaded at startup and saved on exit, if vim is launched in the /projects/projA directory.

Load an alternate viminfo during startup

This is good for the case where you want to save your history to the side and load it later, without managing it at a project level.

First, to save the history, you add to the viminfo option as above prior to exiting. Then to load the history, launch vim with the -i option

vim -i Path/to/custom/viminfofile

If you don't want to exit vim to save the viminfo file, you can use :wv (:wviminfo). This saves the viminfo file without exiting vim according to the n setting above. And, you can also do :wv SomeOtherFile to save to a location that isn't the same as the setting in the viminfo option. This may be more convenient than setting up the filename to use at exit, but doesn't stop vim from using the default file once it does exit.

For example, in first vim:

:wv ~/customInfo

and in second terminal:

vim -i ~/customInfo

Or, if a second vim is already running and you want to load the viminfo:

:rv ~/customInfo

Note that :wv and :rv don't change the name of the file vim will save on exit nor the name of the file read during startup.

Additional information

Description of set viminfo+=nSomePath: We use the set command to adjust the option named viminfo. The += indicates we want to append to the option, not change everything. We append the n option which specifies the filename for viminfo. Immediately after n we have SomePath which will be the filename used for saving viminfo.

The documentation for wv (:help wv) indicates that this command will first read the viminfo file, then merge between old and new information. So far I have no information about how the merging works in this case, but I'd guess it keeps marks for unknown files and registers that aren't set in the current session.

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.