Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

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
share|improve this answer
    
That's a good direction, but there are too many ways to switch panes. I might select-pane, last-pane, swap-pane, or even click a pane. There are just as many ways to switch to a different window. I can't rebind every possible way to activate a different pane. I'm looking, then, for a way to get commands to run every time I activate a pane. Perhaps that's not possible in tmux. –  Peeja Mar 3 '12 at 18:42
add comment

Someone else has already done this work for you in a vim plugin - https://github.com/sjl/vitality.vim

From the README:

(Vit)ality is a plugin that makes (V)im play nicely with (i)Term 2 and (t)mux.

If you're using vim and tmux together you might also be interested in the vimux plugin.

By default when you call RunVimTmuxCommand vimux will create a 20% tall horizontal pane under your current tmux pane and execute a command in it without losing focus of vim. Once that pane exists whenever you call RunVimTmuxCommand again the command will be executed in that pane.

Think re-running tests frequently.

share|improve this answer
1  
Actually, that's not what Vitality does. Vitality maps iTerm's focus lost/gained codes—which trigger when you change GUI windows—into Vim's FocusLost/Gained events, but it doesn't notice if you change tmux windows. –  Peeja Apr 19 '12 at 12:47
    
@Peeja: ah, ok. I misread it, thanks for the clarification. –  Hamish Downer Apr 20 '12 at 7:31
    
vitality.vim now does this with a fork of tmux: github.com/sjl/vitality.vim/issues/2 github.com/aaronjensen/tmux –  Aaron Jensen Feb 19 '13 at 4:49
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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