How to reorder tmux windows?
Short answer
tmux swap-window -s 2 -t 4 # exchange windows 2 and 4
tmux move-window -s 2 -t 4 # move window 2 to index 4 (must be free)
From the keyboard with the default prefix, prefix . opens a prompt running move-window -t, so
you type the new index for the current window.
Details
swap-window exchanges two windows in the list. Their contents stay put, only the index numbers
trade. Add -d so the focus does not follow the moved window. Because it is a swap, no index ever
needs to be free.
move-window is one-directional: it takes a window out of its slot and drops it at a new index.
That index has to be empty. If it is taken, tmux refuses with index in use and changes nothing:
tmux move-window -s 2 -t 0 # index in use: 0 (if 0 already holds a window)
So swap-window is the safe choice when both slots are occupied, and move-window is for sliding
a window into a gap. prefix . is the interactive form of move-window -t: it acts on the current
window and asks for the target index.
To close the gaps left after all this shuffling, renumber-windows repacks the list to 0, 1, 2, ...; that is its own topic.
Verified on tmux 3.2a. With windows 0 1 2 3 4, swap-window -s 1 -t 3 gives 0 3 2 1 4.
move-window -s 4 -t 9 moves window 4 to index 9. move-window -s 2 -t 0 onto an occupied index
returns index in use: 0 and leaves the list untouched. The default binding prefix . runs
command-prompt -T "move-window -t '%%'".