How to reorder tmux windows?

By Romain Dorgueil

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. Because it is a swap, no index ever needs to be free. About focus: on 3.2a, swapping the current window leaves the focus on the current index, so you end up looking at the swapped-in window; add -d to keep looking at the same window from its new index. The -d behaviour has flipped between tmux versions (it changed around 3.0 and again after 3.2), so test it on yours before scripting it.

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.

Related