I use tmux splits (panes). Inside one of these panes there's a Vim process, and it has its own splits (windows).
In Vim I have key bindings C-h/j/k/l
set to switch windows in the given direction. (Vim default mappings for windows switching are the same, but prefixed with C-W
.) I'd like to use the same keystrokes for switching tmux panes.
An extra goal that I've solved with a dirty hack is to toggle between last active panes with C-\
.
Here's how it should work:
- If I'm in "vim window 2", going in left (
C-h
) or down (C-j
) direction should switch windows inside vim. - However, if I'm in "vim window 3", going right (
C-l
) or down (C-j
) should select the next tmux pane in that direction.
The solution has 3 parts:
- In
~/.tmux.conf
, I bind the keys I want to execute a customtmux-vim-select-pane
command; tmux-vim-select-pane
checks if the foreground process in the current tmux pane is Vim, then forwards the original keystroke to the vim process. Otherwise it simply switches tmux panes.- In Vim, I set bindings for the same keystrokes to a custom function. The function tries to switch windows in the given direction. If the window didn't change, that means there are no more windows in the given direction inside vim, and it forwards the pane switching command to tmux by shelling out to
tmux select-pane
.
-
check
tmux -V
that you have v1.8 -
get the vim-tmux-navigator plugin
-
additional
~/.tmux.conf
config &tmux-vim-select-pane
script:curl -fsSL https://gist.github.com/mislav/5189704/raw/install.sh | bash -e
It occurred to me that this could be extended in much the similar way to support the vim-aware creation of windows/panes using a single set of shortcut keys (e.g. Ctrl+\ and Ctrl+- , cf. Ctrl+W,V and Ctrl+A,\ and Ctrl+W,S and Ctrl+A,- for vim window and tmux pane splitting, vertically and horizontally, respectively^2). So that if you are not in vim the shortcuts will make new panes vertically split or horizontally split, and if you are in vim it will make split vim-windows.
I'm not sure if this could ever be worth the trouble as there is the quite conceivable need to make a tmux pane while inside vim. This could obviously be done then with the regular method but then why not just keep using the regular method for all cases? But, for the sake of collapsing shortcuts or creating more overloaded powerful shortcuts, this can be accomplished with the same method.