Reorder tmux windows

swap-window can help you:

swap-window -t -1

It moves current window to the left by one position.

From man tmux:

swap-window [-d] [-s src-window] [-t dst-window]
             (alias: swapw)
This is similar to link-window, except the source and destination windows are swapped. 
It is an error if no window exists at src-window.

You can bind it to a key:

bind-key -n S-Left swap-window -t -1
bind-key -n S-Right swap-window -t +1

Then you can use Shift+Left and Shift+Right to change current window position.


The accepted and highly upvoted answer is correct, though behavior in recent tmux versions sees the swap-window command not keep you on the same window. So it works rather unintuitively, your active window will get swapped in that direction but you will stay put in the same slot!

To fix that issue, simply augment the bind to follow it. For example from my tmux config:

bind -n C-S-Left { swap-window -t -1; previous-window }
bind -n C-S-Right { swap-window -t +1; next-window }

Tags:

Tmux