Where developers come to connect, share, build and be inspired.

32

Seamlessly navigate tmux and vim splits

3501 views

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.

Comments

Add a comment