Last Updated: September 09, 2019
· mislav

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:

  1. tmux first receives the keystroke;

  2. 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);

  3. Vim tries to switch splits in the given direction, and when that's no longer possible it bounces the command back to tmux;

  4. 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.

4 Responses
Add your response

Genius! EXACTLY what I needed :D

over 1 year ago ·

You sure you devised this solution and not christoomey ?

over 1 year ago ·
over 1 year ago ·

@trq see Chri's attribution to @mislav in his GitHub repackaging of this idea:

christoomey / vim-tmux-navigator

over 1 year ago ·