TMUX using HJKL to navigate panes

32.2k views Asked by At

Standard TMUX is set to use ctrl-b + [up, down, left, right] when navigating between panes.

I would like to make it so that I can use ctrl-b (or the prefix of my choice) + [h,j,k,l].

I thought I had done this with the following vi key in my ~/.tmux.conf settings:

set -g status-keys vi
setw -g mode-keys vi

Yet this didn't seem to change anything (at least not what I was looking for). How can I get this to work. And yes my .tmux.conf is working properly. I can provide more info if needed.

Update:

Here is my full .tmux.conf after trying to get it to work:

set -g status-keys vi
setw -g mode-keys vi

set -g prefix C-a
unbind C-b
bind C-a send-prefix


# smart pane switching with awareness of vim splits
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R

Alternatively, I have tried using this w/ vim-tmux-navigator Vim plugin:

# smart pane switching with awareness of vim splits
bind -n C-h run "(tmux display-message -p '#{pane_current_command}' | grep -iq vim && tmux send-keys C-h) || tmux select-pane -L"
bind -n C-j run "(tmux display-message -p '#{pane_current_command}' | grep -iq vim && tmux send-keys C-j) || tmux select-pane -D"
bind -n C-k run "(tmux display-message -p '#{pane_current_command}' | grep -iq vim && tmux send-keys C-k) || tmux select-pane -U"
bind -n C-l run "(tmux display-message -p '#{pane_current_command}' | grep -iq vim && tmux send-keys C-l) || tmux select-pane -R"
bind -n C-\ run "(tmux display-message -p '#{pane_current_command}' | grep -iq vim && tmux send-keys 'C-\\') || tmux select-pane -l"

source

Which also doesn't work either. I am a bit stumped.

4

There are 4 answers

6
Micah Smith On

You can do this as follows:

bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R

Note that mode-keys refers to using vi-like navigation within a buffer and status-keys refers to using vi-like editing within the status bar, but neither refers to switching between panes.

0
Eric S On

Did you remember to source your ~/.tmux.conf file? After making any changes in this file you need to enter the following command to see any of the changes take place

tmux source-file ~/.tmux.conf
0
smoky41 On

The Micah Smith's answer seems to work. But it doesn't have quite the same behaviour that we have with the arrow keys. With the arrows, if you are fast enough, you can hit prefix + arrow key multiple times and you are able to navigate multiple panes, using the same prefix hiy. Main difference:

With the arrows:

// to move 3 panes to the right
prefix + -> -> ->

With this hack:

// to move 3 panes to the right
(prefix + l) 3x

Still, to make this change, you need to update your ~/.tmux.conf file and then restart tmux sessions. To be sure you don't have any tmux sessions you can run

$ tmux list-sessions

If you have some sessions running, run $ killall tmux and you should be good to go.

This☝️ was tested in a macbook, it should be the same for linux.

0
whame On

If you are looking for a modal mode for tmux (e.g. like in Vim text editor), there is a plugin tmux-modal that can be used to execute complex commands with just a few keystrokes. For example:

w h to select the left pane

w j to select the pane below

w k to select the pane above

w l to select the right pane

There is also a sticky mode (w w) that enables h, j, k, l to select the panes as you want to, without a prefix key. Please see the repository for more information.