How to swap two tmux panes?

By Romain Dorgueil

Short answer

tmux swap-pane -s '{left}' -t '{right}'   # exchange the leftmost and rightmost panes
tmux swap-pane -U                         # swap the active pane with the previous one (prefix {)
tmux swap-pane -D                         # ... with the next one (prefix })

swap-pane exchanges two panes without touching the layout: the cells keep their exact position and size, the panes trade places. That is the key difference with join-pane, which breaks a pane out of its slot and recreates a split somewhere else. To flip an uneven left/right split, swap-pane is the right tool: the big cell stays big, the panes switch sides.

Details

-s is the source pane, -t the destination (default: the active pane). Any pane target works: relative tokens like {left}, {right}, {top}, {bottom}, a window.pane form (3.1), a pane id (%7), or the marked pane ({marked}). Quote the tokens, braces mean something to the shell.

prefix { and prefix } run swap-pane -U and swap-pane -D: they exchange the active pane with the previous or next pane in index order, not strictly “the one above”, so the visual effect depends on the layout. Repeat them to carry a pane step by step through the window.

The focus rule is worth knowing. Without -d, the destination pane always ends up focused. That is what makes prefix { feel natural: your pane is the destination, so the focus travels with it. It also means a scripted swap between two other panes steals your focus to the target. With -d, the focus is left alone when you are outside the two panes; when you are inside one of them, it goes to the source pane, so swap-pane -d -U moves your pane up while you stay in your old spot, now showing the other pane.

Verified on tmux 3.2a.

Related