Skip to content
shellhacks.dev
Command Line

Use vi Keys in tmux Copy Mode with mode-keys vi

Switch tmux copy mode to vi keys with set-option -g mode-keys vi. tmux picks vi by itself when EDITOR or VISUAL contains vi at the time the server starts.

2 min read
Summarize in AI

Copy mode in tmux uses emacs keys, and you would rather move and select the way you do in vim.

Shell
tmux -L demo -f /dev/null kill-server 2> /dev/null
EDITOR=nano tmux -L demo -f /dev/null new-session -d
tmux -L demo show-options -g mode-keys
tmux -L demo kill-server
EDITOR=vim tmux -L demo -f /dev/null new-session -d
tmux -L demo show-options -g mode-keys
tmux -L demo set-option -g mode-keys emacs
tmux -L demo set-option -g mode-keys vi
tmux -L demo show-options -g mode-keys
tmux -L demo list-keys -T copy-mode-vi | grep -E 'begin-selection|-and-cancel' | cut -c1-90 | head -n 4
tmux -L demo kill-server
Text
mode-keys emacs
mode-keys vi
mode-keys vi
bind-key -T copy-mode-vi C-j               send-keys -X copy-pipe-and-cancel
bind-key -T copy-mode-vi Enter             send-keys -X copy-pipe-and-cancel
bind-key -T copy-mode-vi Space             send-keys -X begin-selection
bind-key -T copy-mode-vi A                 send-keys -X append-selection-and-cancel

The mode-keys option decides the key table that copy mode uses. In the run, a server started with EDITOR=nano reported emacs and one started with EDITOR=vim reported vi without any configuration, because tmux checks EDITOR and VISUAL for the letters vi. Setting it explicitly in ~/.tmux.conf with set-option -g mode-keys vi removes the dependence on the environment. list-keys -T copy-mode-vi shows the result: Space begins a selection and Enter copies it and leaves copy mode. See tmux.

mode-keys affects copy mode and the choose-tree style menus. The command prompt has its own setting, status-keys. If the server is already running, edit the config and reload it with source-file, because the file is read only at startup. This was run with tmux 3.3a.

Share

More snippets

Get new posts by email

One email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.