Make Tmux tell Vim when its pane loses focus

tmuxvim

I use Vim and Bash side-by-side in Tmux. I have Vim configured to autosave files as I switch between them, but I'd really like it to autosave when I switch to the Bash pane.

Is there a way to get Tmux to send some kind of code to Vim when its pane loses focus?

Best Answer

This is probably best done using vim's client-server model. There's some good guidance about using it in :help remote.txt.

Firstly, you'll need a vim client compiled with the +clientserver option. If your distribution doesn't package it this way, get the source (through apt-get source, abs, et al) and add that option.

Once that's done, you'll need to rebind your keys in tmux so that when you change windows it also sends something like the following to the active window:

vim --servername foo --remote-send '<C-\><C-N>:w<CR>'  

Something like the following should work (not tested):

bind-key 0 run-shell "vim --servername foo --remote-send '<C-\><C-N>:w<CR>'" \; select-window -t :0
Related Question