A custom keybinding in tmux isn’t about changing what tmux does, it’s about changing how you tell it what to do.
Let’s see it in action. By default, to split a pane vertically, you hit Ctrl+b %. To split horizontally, Ctrl+b ". If you want to send a command to all panes, you hit Ctrl+b :.
Now, imagine you want to map these common actions to something more ergonomic, like Ctrl+a as your prefix key (a common alternative to Ctrl+b) and then v for vertical split, h for horizontal, and a to send a command to all panes.
Here’s how you’d achieve that. First, you need to tell tmux to use Ctrl+a as your prefix. You do this in your ~/.tmux.conf file:
set-option -g prefix C-a
bind-key C-a send-prefix # This allows you to send C-a to applications if needed
After saving ~/.tmux.conf, you need to reload the configuration. You can do this by pressing your new prefix (Ctrl+a) followed by :. This enters command mode. Then type source-file ~/.tmux.conf and press Enter.
Now, let’s remap the pane splitting. We’ll use bind-key to associate a key combination with a tmux command.
# Vertical split with Ctrl-a v
bind-key v split-window -h
# Horizontal split with Ctrl-a h
bind-key h split-window -v
# Send command to all panes with Ctrl-a a
bind-key a command-prompt "select-pane -T 'Send to all panes:' ; send-keys -t :.; send-keys \"%%\" ; select-pane -t :."
Add these lines to your ~/.tmux.conf and reload the configuration again (Ctrl+a : source-file ~/.tmux.conf).
Now, with Ctrl+a as your prefix:
- Press
Ctrl+athenvto get a vertical split. - Press
Ctrl+athenhto get a horizontal split. - Press
Ctrl+athenato bring up a prompt where you can type a command that will be sent to every pane.
This is the core of customization: understanding the bind-key command and knowing the tmux commands you want to trigger. You can bind almost any tmux command to any key combination. For instance, if you frequently switch between windows, you might remap Ctrl+b n (next window) and Ctrl+b p (previous window) to something like Ctrl+a n and Ctrl+a p.
# Remap window navigation
bind-key n next-window
bind-key p previous-window
The magic happens because tmux is fundamentally a terminal multiplexer, and its primary interface for interacting with it is through these keybindings. When you press your prefix key, you’re telling tmux, "Hey, the next key I press is for you, not the application running in the current pane." Then, bind-key tells tmux, "When you hear this specific key after the prefix, execute this specific action."
The command-prompt binding for sending commands to all panes is a bit more involved. Let’s break down bind-key a command-prompt "select-pane -T 'Send to all panes:' ; send-keys -t :.; send-keys \"%%\" ; select-pane -t :.":
command-prompt: This tells tmux to enter its command prompt mode after the prefix.select-pane -T 'Send to all panes:': This sets the title of the command prompt to make it clear what’s happening.send-keys -t :.; send-keys \"%%\": This is the crucial part.-t :.refers to the current pane.send-keys \"%%\"takes whatever you type into the prompt and sends it as keystrokes to that pane. The%%is a placeholder for your input.select-pane -t :.: This re-selects the original pane you were on after you finish typing.
This level of detail allows you to craft incredibly specific workflows. You’re not just remapping keys; you’re creating shortcuts for complex sequences of actions, essentially building your own mini-commands within tmux.
Many people get confused about how to send commands to other panes or windows, often thinking it requires external scripting or complex session management. The reality is that send-keys and command-prompt are built-in mechanisms that, when combined with bind-key, allow for sophisticated in-session automation. It’s not about sending raw characters; it’s about telling tmux to simulate those characters being typed.
The next step in customizing tmux is often exploring plugins, which can extend its functionality far beyond what’s available in the default commands.