learntmux.dev

Add and remove key bindings

Goal

A key selected window 10 and was unbound; | and - split; M-n switches windows with no prefix.

Four commands run the whole key system:

bind key command
add a binding you press after C-b
bind -n key command
add one that needs no prefix at all
unbind key
remove a binding
list-keys -T prefix key
show what a key currently does

Bindings live in key tables, and the table decides when a key applies:

prefix
keys pressed after C-b; where bind writes by default
root
keys with no prefix; -n is short for -T root
copy-mode / copy-mode-vi
only inside copy mode

bind replaces whatever the key did before, silently. And a careless -n binding fires on every keystroke, so leave it off unless you mean it.

Do this

Press C-b :, type bind M-0 selectw -t:=10, Enter, then press C-b M-0.

Window 10, far, is current.

Run open ~/.tmux.conf, add these lines, and save with Ctrl-S:

bind | split-window -h
bind - split-window -v
bind -n M-n next-window

Press C-b :, type source ~/.tmux.conf, Enter.

C-b | now splits left and right; plain M-n changes window with no prefix.

Press C-b :, type unbind M-0, Enter.

list-keys -T prefix M-0 now reports an unknown key.

Exact window targets

In -t :=10, the : says the target is a window and the = demands an exact match on index 10 — without it, 10 would also match a window whose name merely starts that way.

In the configuration file | needs no quoting; only the shell treats it specially.

Stuck on this one?

This is a live terminal. Typing goes to the machine, including Tab. Press Shift and Tab together to move focus back out to the page.

A real terminal, in this tab

about 15 MB, once

session learn

Done when

not started
  1. Some key in the prefix table selects window 10— not done yet
  2. Window 10 is now the current window of session learn— not done yet
  3. The | key in the prefix table splits the window left/right— not done yet
  4. The - key in the prefix table splits the window top/bottom— not done yet
  5. M-n in the root table runs next-window— not done yet
  6. M-0 no longer has a binding in the prefix table— not done yet