How to target tmux windows with special tokens?

By Romain Dorgueil

Short answer

tmux select-window -t '{end}'              # jump to the highest-numbered window
tmux select-window -t '{start}'            # jump to the lowest-numbered window
tmux select-window -t '!'                  # toggle back to the previously active window
tmux swap-window -s '{start}' -t '{end}'   # tokens work anywhere a window target does

Anywhere a command takes a window target (-t or -s), you can use a special token instead of an index or a name: {start} and {end} are the lowest and highest numbered windows, {last} is the previously active one, and {next} and {previous} are the neighbours by number.

Details

Each token has a one-character alias: ^ for {start}, $ for {end}, ! for {last}, + for {next} and - for {previous}. The last two accept an offset: +2 means two windows ahead, -2 two behind.

Quote the tokens in a shell. {, $ and ! all mean something to it: an unquoted {end} survives by luck, but $ starts a variable expansion and ! triggers history expansion in an interactive shell. Single quotes cover every case.

The point of tokens is scripting: select-window -t '{end}' keeps working whatever the actual indexes are, with base-index 1, after windows were closed, or after a renumbering. Interactive equivalents already exist as keys (prefix n, prefix p and prefix l walk next, previous and last); the tokens give the same moves to scripts and to one-off commands. They also resolve before the number rules apply, so unlike a bare number they can never be misread as a pane index.

Verified on tmux 3.2a.

Related