Seamlessly navigate tmux and vim splits
Between tmux panes and Vim splits, it's difficult to keep track of the current context to know which key bindings to use for navigating splits. For a long time, I've used <C‑h/j/k/l>
keystrokes inside Vim to switch to the next split in the given direction, and I wanted to use the same for tmux panes. However, if I simply bound the same keys in tmux, they would stop working in Vim.
I've devised a solution that configures tmux and Vim to cooperate in this regard. It works roughly like so:
tmux first receives the keystroke;
If the current tmux pane contains vim running in the foreground (
tmux display -p '#{pane_current_command}'
), tmux forwards the keystroke to it (tmux send-keys
);Vim tries to switch splits in the given direction, and when that's no longer possible it bounces the command back to tmux;
tmux switches the pane in the given direction.
With this I can navigate splits without having to pay attention whether they come from vim or tmux. It's liberating.
Written by Mislav Marohnić
Related protips
4 Responses
Genius! EXACTLY what I needed :D
You sure you devised this solution and not christoomey ?
Read the first paragraph
@trq see Chri's attribution to @mislav in his GitHub repackaging of this idea: