How to change the prefix key in tmux?

Quick Answer

To change the tmux prefix key from the default Ctrl+b, add these lines to your ~/.tmux.conf file: unbind C-b to remove the default binding, then set -g prefix C-a to set a new prefix (in this example, Ctrl+a), and finally bind-key C-a send-prefix to allow sending the prefix to nested tmux sessions. After saving, reload your configuration with Ctrl+b : source-file ~/.tmux.conf or restart tmux.

# Add to ~/.tmux.conf
unbind C-b
set -g prefix C-a
bind-key C-a send-prefix

Detailed Explanation

The prefix key in tmux is the key combination you press before entering any tmux command. By default, this is Ctrl+b, but many users change it for comfort, to avoid conflicts with other programs, or for consistency with GNU Screen (which uses Ctrl+a).

Why Change the Prefix Key?

Common reasons to change the default prefix:

  • Ergonomics: Some find Ctrl+a easier to reach than Ctrl+b
  • Muscle memory: Users coming from GNU Screen are accustomed to Ctrl+a
  • Avoiding conflicts: Ctrl+b is used in some programs (like Vim for page up)
  • Personal preference: Some keys may feel more natural based on your keyboard layout

Step-by-Step Configuration

To change your prefix key, follow these steps:

1. Edit your tmux configuration file:

# Open ~/.tmux.conf in your preferred editor
vim ~/.tmux.conf
# or
nano ~/.tmux.conf

2. Add the configuration lines:

# Remove the default prefix
unbind C-b

# Set new prefix key (Ctrl+a in this example)
set -g prefix C-a

# Allow sending the prefix key to applications by pressing it twice
bind-key C-a send-prefix

The third line is particularly important if you need to send the actual Ctrl+a keystroke to applications inside tmux. With this binding, you can press Ctrl+a twice to send one Ctrl+a to the current program.

3. Apply the changes:

# If tmux is already running, reload the configuration
# First press Ctrl+b (the old prefix), then type :source-file ~/.tmux.conf
Ctrl+b :source-file ~/.tmux.conf

# Or restart tmux
tmux kill-server
tmux

After reloading, your new prefix key will be active. Remember to use the new prefix for all tmux commands going forward.

Popular Alternative Prefix Keys

Here are some common alternatives to the default Ctrl+b prefix:

  • Ctrl+a - Popular choice, especially for Screen users
  • Ctrl+space - Doesn't conflict with many applications
  • Alt+a - Leaves Ctrl combinations free for other applications
  • Backtick ` - Single key option (requires different handling)

Using a Single Key as Prefix

If you want to use a single key like backtick (`) as your prefix:

# Set backtick as the prefix
unbind C-b
set -g prefix `
bind-key ` send-prefix

# Optional: Use a key like F12 as an alternative prefix
set -g prefix2 F12
bind-key F12 send-prefix -2

When using a character key as prefix, you'll need to press it twice to send it to applications.

Working with Nested tmux Sessions

If you frequently work with nested tmux sessions (tmux inside tmux), consider using different prefix keys for each level:

# For your local ~/.tmux.conf
set -g prefix C-a

# For the remote ~/.tmux.conf
set -g prefix C-b  # Keep the default on remote servers

This helps avoid confusion about which tmux session you're sending commands to.