How to target the right tmux pane by number?

By Romain Dorgueil

Short answer

tmux swap-pane -s 3      # ambiguous: pane 3 of THIS window, or window 3?
tmux swap-pane -s 3.1    # unambiguous: window 3, pane 1
tmux swap-pane -s %7     # unambiguous: pane id, never reinterpreted

A bare number in a pane target is read as a pane of the current window when a pane with that index exists, and otherwise as a window (tmux then acts on that window’s active pane). Because the meaning depends on how many panes the current window has, -s 3 can switch between “pane 3 here” and “window 3” with no warning. Qualify the target and the ambiguity is gone.

Details

The safe forms are a window.pane pair (3.1), a pane id (%7, shown in #{pane_id} and by list-panes), or a window name (build.1). A pane id is the strongest: it is stable for the life of the pane and is never reinterpreted as a window.

This bites hardest on the commands that move panes around, swap-pane and join-pane, where the short form -s 3 is tempting. It works only while the current window has fewer panes than the number you type; the day it grows one more pane, the same command starts acting on a different window instead.

Verified on tmux 3.2a.

Related