Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass commands from Tmux to Vim

Tags:

vim

tmux

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
like image 924
nbwoodward Avatar asked Oct 30 '25 00:10

nbwoodward


1 Answers

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.

like image 135
jeremysprofile Avatar answered Nov 02 '25 07:11

jeremysprofile



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!