Change the tmux prefix key from Ctrl+B to Ctrl+A or another key
Set a new tmux prefix in ~/.tmux.conf with set -g prefix, unbind C-b and bind send-prefix, then reload with source-file. Includes keys that clash.
tmux listens for a prefix key before each of its commands: Ctrl+B, then d to detach, c for a new window, and so on. If Ctrl+B is awkward to reach, or you are used to screen's Ctrl+A, change it.
Set the new prefix
Put these lines in ~/.tmux.conf on the host (create the file if it does not exist):
set-option -g prefix C-a
unbind-key C-b
bind-key C-a send-prefix
- The first line makes
Ctrl+Athe prefix. - The second removes the old
Ctrl+Bbinding, so Ctrl+B goes to the program again. - The third means that pressing the prefix twice sends one Ctrl+A to the program in the pane.
This is the example in the tmux manual.
Load it
tmux reads ~/.tmux.conf once, when its server starts. A server that is already running keeps the old settings until you load the file into it:
tmux source-file ~/.tmux.conf
Run that from a shell inside tmux, or press the current prefix and : and type source-file ~/.tmux.conf. New sessions on the same server get the change too.
Keys that clash
Any key you make the prefix stops going straight to programs. Common choices and what they take away:
- Ctrl+A is also "go to start of line" in Bash's line editor. With the
send-prefixline above, press Ctrl+A twice to get it. - Ctrl+Space sends a NUL byte, which some keyboards and terminals do not send at all: test it first, as in Ctrl+Space not working over SSH or in tmux: the NUL byte and how to test it.
- The backtick key is a single keypress on most hardware keyboards, but backticks are common in shell commands. If you use it, keep the
send-prefixline so that pressing it twice types a real one (example below).
A backtick prefix in ~/.tmux.conf:
set-option -g prefix '`'
unbind-key C-b
bind-key '`' send-prefix
Keep two prefixes
Instead of replacing Ctrl+B, you can add a second prefix with prefix2 and keep Ctrl+B while you get used to the new key:
set-option -g prefix2 C-a
bind-key C-a send-prefix -2
send-prefix -2 sends the secondary prefix to the program.
tmux inside tmux
When you run tmux on a host from inside tmux on another machine, both use the same prefix and the outer one catches it. Press the prefix twice to reach the inner session, or give the inner one a different prefix. See tmux "sessions should be nested with care, unset $TMUX to force".
Related
- Shell and tmux basics
- Keep your work after a disconnect with tmux, and attach automatically
- Ctrl+C copies instead of interrupting, or Ctrl+V and Ctrl+A never reach the host
- App guide: Keyboard shortcuts