If you spend a lot of time in a terminal, tmux
"copy mode" is useful.
It lets you move around the terminal via vim
-like keyboard shortcuts,
scrolling and copying terminal contents without reaching for a mouse.
However, even when you set the tmux
mode-keys
to vi
, many of the keybindings
are quite different from vim
: you e.g. activate it with Ctrl+b
[, start selecting with Space, and yank with Enter.
In contrast, vim
enters normal mode with Esc, starts selecting with
v, and yanks with y. There are other minor differences that
can be confusing. This plugin makes tmux
copy mode more similar to vim
normal
mode, thus reducing context switching.
To install this plugin via the Tmux Plugin Manager, just add this to tmux.conf
:
set -g @plugin 'jabirali/tmux-normalmode'
Specifically, these are the keybindings mapped by this plugin:
Keybinding | Description |
---|---|
Esc | Enter "normal mode" |
i or a | Enter "insert mode" |
y | Yank selection |
v | Select characters |
Ctrl+v | Select rectangle |
Ctrl+p or [ | Previous shell prompt |
Ctrl+n or ] | Next shell prompt |
Above, "normal mode" refers to tmux
copy mode, where vim
-like keybindings
apply. "Insert mode" is the tmux
root mode, i.e. the default mode in tmux
.
Most other relevant vim
keybindings such as Shift+v,
Ctrl+u, Ctrl+d, are already supported
by tmux
by default. See man tmux
, specifically under WINDOWS AND PANES
.
The Esc binding tries to be smart: it activates copy mode if the
current command appears to be a shell (e.g. bash
or fish
), but passes the
Esc binding on to the running program if not (e.g. vim
or man
).
This is because when you are not in a shell, you will most likely be in an
interactive (e.g. curses
-based) app, and many of those use Esc.
The binding is disabled by default, since people that use vi
bindings in
their shell may not want it enabled. To enable it, you have to specify a
whole-line regexp that matches your shell. For instance, to match any
command like bash
or fish
that end in sh
, set this regexp to:
set -g @normalmode-regexp '[a-z]*sh.*'
If you want to enter normal mode in a app that does not match the pattern above,
you can either use the default Ctrl+b [, or you can
map your own in tmux.conf
. For instance, I like Alt+y:
bind -n 'M-y' copy-mode
As a bonus feature, if you have a distinctive set of characters in your shell
prompt like ❯
or >>>
, you can get keybindings to navigate between them.
To enable these, add a setting to your tmux.conf
describing your prompt:
set -g @normalmode-prompt '❯'