rwmdvq

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

7689
F93e90d4db9dc0eb3b5681c6fccfa0e2

Genius! EXACTLY what I needed :D

over 1 year ago ·
7741
Baf2401c60cebda2259c84ef825569f6

You sure you devised this solution and not christoomey ?

over 1 year ago ·
7742
8f93a872e399bc1353cc8d4e791d5401
over 1 year ago ·
12096
808e1f311009f0e3e3115136775830db

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

christoomey / vim-tmux-navigator

over 1 year ago ·