How to renumber tmux windows and close the gaps?
Short answer
tmux move-window -r # repack now: indexes become 0, 1, 2, ...
set -g renumber-windows on # ~/.tmux.conf: repack on every window close
move-window -r renumbers all windows in the current session right away. The config line keeps
them packed automatically from then on.
Details
Close a window in the middle of the list and tmux leaves its index empty, so you drift to
something like 0 3 7. The numbers still work, but prefix 2 no longer lands on the third
window, and the gaps are just noise.
move-window -r (the -r is “renumber”) walks the session and reassigns every window to a
contiguous range starting at base-index, which is 0 by default. It is a one-shot cleanup you
run when the list has gotten messy.
renumber-windows on in ~/.tmux.conf makes tmux do the same thing on its own every time a
window closes, so the list never grows gaps in the first place. It pairs well with base-index 1
if you number windows from 1 to match the keyboard.
# ~/.tmux.conf
set -g renumber-windows on
Verified on tmux 3.2a. Windows at 0 3 7, after move-window -r, become 0 1 2. With
renumber-windows on and windows 0 1 2 3, closing index 1 leaves 0 1 2 instead of 0 2 3.