Skip to content
shellhacks.dev
Tooling

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.

2 min read
Summarize in AI

Two panes are in the wrong order, and closing one to reopen it in the other place would lose what is running.

Shell
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-server
Text
index 0 id %0 title left
index 1 id %1 title right
---
index 0 id %1 title right
index 1 id %0 title left

swap-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.

Share

More snippets

Tooling

Zoom a pane to full screen

Zoom one pane to fill the window with resize-pane -Z, or prefix then z, and call it again to restore. The window shows a Z flag while zoomed.

1 min read
Tooling

Split a pane

Split a tmux pane with split-window -h for side by side and -v for stacked. The key bindings are prefix then percent and prefix then a double quote.

1 min read
Tooling

Resize a pane

Resize a pane with resize-pane -x 30 for an exact width or -R 10 to move its edge. The keys are prefix then Ctrl and an arrow, one cell at a time.

1 min read

Get new posts by email

One email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.