Swap two panes in a window
Swap two panes in a window with swap-pane -s source -t target. The panes keep their IDs and running programs and trade positions, and prefix then braces do it.
Two panes are in the wrong order, and closing one to reopen it in the other place would lose what is running.
tmux kill-server 2>/dev/null; sleep 1
tmux new-session -d -s w -x 120 -y 30
tmux split-window -h -t w
tmux select-pane -t w:0.0 -T left
tmux select-pane -t w:0.1 -T right
tmux list-panes -t w -F 'index #{pane_index} id #{pane_id} title #{pane_title}'
echo ---
tmux swap-pane -s w:0.0 -t w:0.1
tmux list-panes -t w -F 'index #{pane_index} id #{pane_id} title #{pane_title}'
tmux kill-serverindex 0 id %0 title left
index 1 id %1 title right
---
index 0 id %1 title right
index 1 id %0 title leftswap-pane exchanges the two panes' places in the layout. The pane ID, such as %0, belongs to the pane and its process for its whole life, so after the swap the same IDs and titles appear at the other index. That is the way to tell a swap from a restart. The keys are prefix then { to move the active pane up in the order and prefix then } to move it down, listed in the tmux manual.
The panes swap positions but not sizes, so a wide pane may end up narrow, and select-layout can even them. -d keeps the active pane where it was, otherwise focus moves with the swap. A program can overwrite the title you set with select-pane -T through an escape sequence.