2
votes

I'm currently using this on my .tmux.conf

bind -n C-h if "[ $(tmux display -p '#{pane_current_command}') = vim ]" "send-keys C-h" "select-pane -L"
bind -n C-j if "[ $(tmux display -p '#{pane_current_command}') = vim ]" "send-keys C-j" "select-pane -D"
bind -n C-k if "[ $(tmux display -p '#{pane_current_command}') = vim ]" "send-keys C-k" "select-pane -U"
bind -n C-l if "[ $(tmux display -p '#{pane_current_command}') = vim ]" "send-keys C-l" "select-pane -R"

This allows me to just press: ctrl + [hjkl] and I can move like in vim (left, down, up, right), but while entering copy mode in tmux:

ctrl + b + [

my bindings stop working and if I want to move I need to use the arrow keys:

ctrl + b + arrow keys

Any idea off how to keep the vim bindings work in copy mode or to make them permanent in whatever mode is use?

I would like to move across panels under copy mode, don't confuse this with trying to copy text vim-like.

3

3 Answers

3
votes

I had the same issue. It appears to be an issue with tmux 2.4. You can check out this issue on the plugin mentioned on a previous response (https://github.com/christoomey/vim-tmux-navigator/issues/159).

In short, just add this to your .tmux.conf

bind-key -T copy-mode-vi C-h select-pane -L
bind-key -T copy-mode-vi C-j select-pane -D
bind-key -T copy-mode-vi C-k select-pane -U
bind-key -T copy-mode-vi C-l select-pane -R
bind-key -T copy-mode-vi C-\ select-pane -l
0
votes

This is partly built into tmux and with a couple of key bindings you can get vim keys for the most part.

# turn on vi key mode
setw -g mode-keys vi

# bind v, y, Escape keys in vi-copy mode
bind -t vi-copy v begin-selection
bind -t vi-copy y copy-selection
bind -t vi-copy Escape cancel

# bind <prefix> + p to "paste"
unbind p
bind p paste-buffer

Now when you enter copy mode, you can use vim keys to move the cursor around and make selection/copy work.

h,j,k,l - movement of cursor
C-b     - page up
C-f     - page down
0,$     - start or end of current line
v       - enter select mode
V       - enter line-based select mode
y       - yank current selection into copy buffer
Escape  - cancel copy mode

Additionally, once out of copy mode, the <prefix + p binding above will allow you to use <prefix> + p to paste the copy buffer.

0
votes

I´m using a plugin for navigating between panes:

https://github.com/christoomey/vim-tmux-navigator

The author of this plugin uses this snippet in ~/.tmux.conf

# Smart pane switching with awareness of Vim splits.
# See: https://github.com/christoomey/vim-tmux-navigator
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"
bind-key -n C-\ if-shell "$is_vim" "send-keys C-\\" "select-pane -l"

It works for me also in tmux copy mode