40

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

5 Answers 5

62

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? Commented Aug 21, 2014 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. Commented Aug 21, 2014 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. Commented Aug 21, 2014 at 8:19
  • see unix.stackexchange.com/a/525770/12497 for addressing the changed behavior of swap-window in newer tmux versions Commented Jun 19, 2019 at 5:44
  • 7
    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 Commented Feb 12, 2020 at 2:06
16

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
  • 10
    A better way is to use -d to restore the old intuitive behavior. E.g. swap-window -d -t -1. Source: github.com/tmux/tmux/issues/1879 Commented Feb 12, 2020 at 2:00
11

Out-of-the-box way (Tmux 3.0+)

There is this display-menu feature in changes from v2.9 to v3.0.

In Tmux, you can now use (Prefix) < to trigger a floating box, where you can control to swap the current window with others as below.

display-menu-demo


Remarks:

  • The Prefix by default is Ctrl-b
  • (Prefix) > will trigger a floating box with other features too, such as splitting and zoom.

I accidentally stumbled upon the shortcut of using (Prefix) < to trigger a floating box, where you can control to swap the current window with others.

Not sure if it is a new feature since I couldn't find it in any documentation while having all plugins disabled. But it helped me to reorder the windows.

Thank you for quoting the documentation by @KamilMaciorowski

1
  • 1
    This should be the correct answer in 2024 Commented Oct 8, 2024 at 10:48
2

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
  • 1
    Brilliant! Thank you. Commented Nov 4, 2019 at 19:42
1

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.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.