29

How can I efficiently reorder windows in tmux? For example, having this set of windows:

0:zsh  1:elinks  2:mutt  3:irssi  4:emacs  5:rss  6:htop

What would I have to do to move rss to between elinks and mutt, ending up with:

0:zsh  1:elinks  2:rss  3:mutt  4:irssi  5:emacs  6:htop

I know how to use move-window to move a window to a yet-unused index, and I could use a series of them to achieve this—but, obviously, this is very tedious.

1
50

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.

5
  • But I would still have to do a series of swap-window commands (three for the example case)—correct? – igor Aug 21 '14 at 8:10
  • @igor: Yes. Another way, you can do swap-window -s 2 -t 5. This swap window number 2 with window number 5. – cuonglm Aug 21 '14 at 8:13
  • Right—but I want to keep the order of all other windows intact, so a ‘simple’ swapping of windows #2 and #5 doesn’t do it for me. I already was aware of the swap-window command, but didn’t know about the -/+ prefixes to target the previous/next window. – igor Aug 21 '14 at 8:19
  • see unix.stackexchange.com/a/525770/12497 for addressing the changed behavior of swap-window in newer tmux versions – Steven Lu Jun 19 '19 at 5:44
  • 3
    In new tmux versions, add the -d option for a more intuitive behavior where you keep focus on the swapped window. E.g. swap-window -d -t -1 – Jerome Dalbert Feb 12 '20 at 2:06
10

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 }
1
1

Here is a working solution wrapped in bash function.

list=$(tmux lsw -F '#I');                           # for shift left, could be moved inside function
list=$(tmux lsw -F '#I'); list=$(echo $list|rev);   # for shift right, list reversed
shift_tmux_window_range() {  # depend on `list`
    local started=0 beg=$1 end=$2
    for i in $list; do
        if (($i == $beg)); then
            started=1; h=$beg; tmux linkw -s $beg && tmux unlinkw -t $beg;
        elif (($i == $end)); then
            tmux movew -d -s $i -t $h; tmux movew -d -t $end; break;
        elif [ $started = 1 ]; then
            tmux movew -d -s $i -t $h; h=$i;
        fi;
    done
}

shift_tmux_window_range 5 2

Bonus: stay in the same window if current active window not affected.

1
0

If tmux is version 1.7 or higher

move-window -r

or

set-option -g renumber-windows on

in .tmux.conf for automatically doing, in future.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.