vim-tmux-navigator to use tmux prefix instead of C-[hjkl]

I have installed vim-tmux-navigator on the tmux and the vim side and I can navigate between vim panes and tmux windows using Ctrl-[hjkl]

BUT: This means that readline (or bash?) shortcuts are overwritten, e.g. Ctrl-k (delete to end-of-line), which I want to use.

All my attempts to remap keys to require the the tmux-prefix, e.g. Ctrl-b [hjkl] vim-tmux-navigator have failed.

Does anyone have a solution for this?

Note: I am using C-b as an example, for which there is a good solution below. I have been using C-Space wich complicates things a little.


Solution 1:

Yes, it's possible to remap the keybindings in order to use <C-b> (your tmux prefix) followed by [hjkl] to change panes, and \ to go to the previous pane.

Note that you need to configure that both in Vim and in tmux.

This is the Vim configuration for those keybindings:

let g:tmux_navigator_no_mappings = 1

nnoremap <silent> <C-b>h :TmuxNavigateLeft<cr>
nnoremap <silent> <C-b>j :TmuxNavigateDown<cr>
nnoremap <silent> <C-b>k :TmuxNavigateUp<cr>
nnoremap <silent> <C-b>l :TmuxNavigateRight<cr>
nnoremap <silent> <C-b>\ :TmuxNavigatePrevious<cr>

Add those lines to your .vimrc.

And in your .tmux.conf, use the snippet (not the TPM) to configure it, then change the main bindings to:

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

(In short, remove the C- part, and also the -n which makes them run without a prefix. Then update the send-keys to send the keys Vim is expecting, which are the same ones, with the prefix.)

You might want to update the copy-mode-vi keybindings too, though it's unclear which keys you would like to use there, since there's no "prefix" enabled in that mode... You might want to think about that one.