3
votes

I'm using Ctrl-[vim movement keys] to change panes in Tmux (C-j moves to the pane below, etc.). Often I have a Vim window open with several splits. Is there a way for tmux to recognize vim is open in a pane, and to pass the "select the next vim split down" with C-j? Maybe it would look something like this:

bind -t C-j
    if in vim
        C-W-J # move to next vim split down
    else
        select-pane -D # move to next tmux pane down
1
It would be great if tmux provided a native way to do this. A hack I use for this sort of thing is to define separate key tables and enable/disable them appropriately as needed. That is, make vi a wrapper which changes the tmux keytable, then opens the file, and resets the keytable on exit. Or you could do the keytable change in vim startup/shutdown scripts.William Pursell
That would be nice.. Maybe something to try to contribute.nbwoodward

1 Answers

5
votes
is_vim="ps -o state= -o comm= -t '#{pane_tty}' \
    | grep -iqE '^[^TXZ ]+ +(\\S+\\/)?g?(view|n?vim?x?)(diff)?$'"

bind-key -n C-h if-shell "$is_vim" "send-keys C-h"  "select-pane -L"
bind-key -n C-j if-shell "$is_vim" "send-keys C-j"  "select-pane -D"
bind-key -n C-k if-shell "$is_vim" "send-keys C-k"  "select-pane -U"
bind-key -n C-l if-shell "$is_vim" "send-keys C-l"  "select-pane -R"

As seen in Chris Toomey's Vim-tmux-navigator

Obviously you'll need to change your send-keys to match whatever moves you through your vim panes.