Search code examples
gitvimtmuxvimdiff

Switching panes within Git's Vimdiff in Tmux


I'm using vim with tmux.

I setup seemless pane switching between Vim splits and Tmux splits

With commands like this:

bind -n C-h run "(tmux display-message -p '#{pane_current_command}' | grep -iq vim && tmux send-keys C-h) || tmux select-pane -L"

In my tmux config and it works great.

However, it breaks when I use git to start vim. For example:

git difftool --tool=vimdiff --no-prompt filename.rb

This command starts vim, but since it's through git seemless pane switching doesn't work because it looks to see is the pane is running process "vim" and it is not (it registers as "git").

Does anyone know of a way to fix this?


Solution

  • Here is my solution, it's a bit hacky but it works

    I created a file called ~/vdif

    and then an alias alias vdif="~/vdif so I can call it from wherever

    In this file I put the following:

    printf "\033]2;%s\033\\" "vim"
    git difftool --no-prompt --tool=vimdiff $1 $2
    printf "\033]2;%s\033\\" "bash"
    

    The first line sets the pane_title to "vim", the second opens the git diff, the third lines sets page title to "bash" upon exit.

    Then in my tmux config I added the following to check both the pane_title and pane_current_command option to see if vim exists:

    is_vim='echo "#{pane_current_command}#{pane_title}" | grep -iqE "vim"'
    bind -n C-h if-shell "$is_vim" "send-keys C-h" "select-pane -L"
    bind -n C-j if-shell "$is_vim" "send-keys C-j" "select-pane -D"
    bind -n C-k if-shell "$is_vim" "send-keys C-k" "select-pane -U"
    bind -n C-l if-shell "$is_vim" "send-keys C-l" "select-pane -R"
    bind -n C-\ if-shell "$is_vim" "send-keys C-\\" "select-pane -l"