8

Can I share Vim command history across instances in real time?

By that I mean what people do in bash this way:

export PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND"

That is, when I run a command echo test1 in one shell, and then press Up in another one, I see echo test1. (To be precise, one has to press Enter before Up for it to work.)

Is this possible with Vim? At first I thought using the +clientserver option would work out. But that seems to be something else.

1

2 Answers 2

14

The command history is stored in the viminfo file (:help viminfo). Usually that is read on startup and written on exit, but you could explicitly persist and sync between Vim instances with a combination of :wviminfo and :rviminfo. Note that this will sync the entire information; i.e. also register contents, marks, buffer lists, etc.

1
1

In Vim the closest you can get is (as Ingo's answer and Christian's comment mentioned) is:

augroup viminfo
    autocmd!
    autocmd FocusLost   * wviminfo
    autocmd FocusGained * rviminfo
augroup end

It's important to realize that the Focus* autocommands aren't always run when you use Vim from a terminal, especially not if you're using tmux or screen. You should test this first by using :au FocusLost :echoerr "Focus lost!".

As far as I know there is no good work-around if these autocommands don't work for you. Using other events (such as CursorHold) seem problematic to me due to timing issues that will almost certainly crop up.


Neovim comes with "ShaDa", which is a replacement for Vim's viminfo system. One aspect where it's better is merging files from different instances, and while it doesn't directly support syncing between multiple instances (yet), this seems to work well for at least one Neovim developer:

augroup shada
  autocmd!
  autocmd CursorHold * rshada | wshada
augroup end

See :help shada-merging for details on the merging process.

3
  • Note: I think Vim 8 also introduce merging functionality because it timestamps the commands entered. See :h viminfo-timestamp May 24, 2017 at 19:29
  • I tried it on my system before posting this answer @ChristianBrabandt, and it didn't seem to work very well. Maybe I did something wrong. May 24, 2017 at 19:30
  • perhaps a bug? would need a good reproducible example at @vim-dev list May 24, 2017 at 21:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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