Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In screen, I can just type C-a :number 0 to move a window to the top of the window list and push all the other windows down one. What's the equivalent command sequence for tmux? I looked at the man page, but I'm finding it confusing on this point.

share|improve this question

migrated from stackoverflow.com Oct 6 '11 at 10:36

This question came from our site for professional and enthusiast programmers.

27  
C-b, . lets you renumber a window. – isomorphismes Feb 19 '14 at 1:01
2  
Reading all the many answers, I still see no easy solution to the OP's question: how to move a given window to position 0 and shift all the rest to the right. Do I really have to manually shift each one to do this? I just want to undo a move I did by mistake (and I'm not sure just what) which I think moved window 0 to window 8 and shifted all the others to the left. – nealmcb Dec 25 '14 at 20:07

13 Answers 13

up vote 233 down vote accepted

The swap-window command is closest to what you want.

"Prefix :" (that is "Ctrl-B :" by default) brings you to the tmux-command prompt. There you enter:

swap-window -s 3 -t 1

to let window number 3 and window number 1 swap their positions.

To swap the current window with the top window, do:

swap-window -t 0

In the unlikely case of having no window at index 0, do:

move-window -t 0

(if base-index is 0, as it is by default).

You can bind that command to a key (T for "top" for example) by adding the following to your ~/.tmux.conf:

bind-key T swap-window -t 0
share|improve this answer
3  
Thanks for your edit, but move-window only works, if there is not another window at the given index. So in most cases, move-window -t 0 will not work, since usually there will be another window at that position already. – matlehmann Mar 21 '13 at 15:32
    
That's right, I will improve it. – zakkak Mar 23 '14 at 23:31
1  
You might want to have a look at movew. – Marcello Romani Feb 19 '15 at 9:57

Adding to Gareth's answer, you can use the following key bindings

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

Pressing Ctrl+Shift+Left (will move the current window to the left. Similarly right. No need to use the modifier (C-b).

share|improve this answer
    
This -1 and +1 syntax helps solve the problem of occupied indices. tmux will shift the other windows for you and will event wrap around to the end/beginning automatically. This is the best answer. – brianpeiris Oct 10 '13 at 13:29
2  
This is godly! Thanks. – Bruno Buccolo Jan 21 '14 at 13:56
2  
Great hint, prefer bind-key S-Left swap-window -t -1, so I can do <prefix> shift+arrow, as I don't like messing up key binds for programs. – demure Jun 1 '14 at 0:35
2  
If using letters you need to use a capital letter and not C-S, like this: bind-key -n C-H swap-window -t -1 – Matthew Mitchell Dec 19 '14 at 15:28
1  
I have some problem on 1.9a as well when using Ashish Ariga bind-key -n C-S-Left swap-window -t -1 however it works ok when using the prefix as suggested by @MatthewMitchell – Roberto Franceschini Aug 21 '15 at 8:09

The tmux equivalent to :number 42 is :move-window -t 42.

share|improve this answer
6  
This is bound to C-b . – vedang Jun 30 '11 at 12:59
1  
Thanks! This is much better than the accepted answer imo. – Yuki Izumi Feb 14 '12 at 22:27
4  
@ArlenCuss Actually both answers are good and useful. You see, using screen :number you could swap windows, using tmux's :move-window you can only relocate window if the target index is not in use. So, imo, both :swap-window and :move-window are necessary to grasp control over window locations :) – Victor Farazdagi Mar 20 '12 at 12:30
1  
+1 for getting an answer from a programming guru (also for being correct). This is the only way I could get it to work on an active window – engineerDave Jul 24 '12 at 13:37
4  
tmux's :move-window is not equivalent to screen's :number. :number swaps if the destination exists, :move-window fails in that case. You have to choose between :move-window and :swap-window – piec Jan 29 '13 at 23:28

You can implement an equivalent to screen's number command using an external shell script that chooses between swap-window and move-window. You can bind it to a key that way:

bind < command-prompt -p index "run-shell '~/.tmux.number.sh %%'"

~/.tmux.number.sh:

#!/bin/bash
if [ $# -ne 1 -o -z "$1" ]; then
    exit 1
fi
if tmux list-windows | grep -q "^$1:"; then
    tmux swap-window -t $1
else
    tmux move-window -t $1
fi
share|improve this answer
2  
Very nice solution, working great! I just made minor edits to cleanup and removed the -F option which is not accepted by my tmux version. – haridsv Jan 23 '13 at 9:33

Using swap-window to move to any id: [closest to screen's :number]

# window movement / renumbering like in screen's :number
bind-key m command-prompt -p "move window to:"  "swap-window -t '%%'"

[m for move --> hit prefix-m and enter say 3 . .to renumber window to 3]

share|improve this answer
1  
This is already nice, but even better would be: do swap-window and if it fails, fall back to move-window – nisc Dec 26 '11 at 15:18

I renumber windows like this:

Ctrl+b, ., 222

would make the current tmux window (all panes) number 222.


Relatedly: When I'm shuffling things around I tend to want to do

Ctrl+b :new-session -d -s "reading"

and from there I can also use Ctrl+b, ., reading to move the current window (all panes at once) over to the reading session. You can browse among sessions with Ctrl+b, s the way you would browse within session using Ctrl+b, w.

HTH

share|improve this answer

The approach I use combines a bit of Ashish's answer with piec's; I have alt-left and right arrow bound to a quick little shell callout that moves the window one to the left or the right, unless it is the first or last window, respectfully. I did this because, when you issue a swap +1 at the last window (or swap -1 at the first window), it will still swap, instead of looping back around again like you might expect:

0:one 1:two 2:three 3:zero*

Becomes

0:zero* 1:two 2:three 3:one

Instead of

0:zero* 1:one 2:two 3:three

So, the commands I use stop working when the window has reached the edge of the list:

bind-key -n M-Left run-shell 'tmux list-windows | head -n 1 | grep -q active || tmux swap-window -t -1'
bind-key -n M-Right run-shell 'tmux list-windows | tail -n 1 | grep -q active || tmux swap-window -t +1'

This can easily be combined with base-index and renumber-windows to have a list of windows that start at an arbitrary number and never has any gaps.

If you are using base-index 1 like me and you don't think you'll ever go above 999 windows, you can use a little trick to make it roll properly, though the commands bloat a bit:

set -g base-index 1
set -g renumber-windows on
bind-key -n M-Left run-shell 'if tmux list-windows | head -n 1 | grep -q active ; then tmux move-window -t 999 \; move-window -r \; refresh-client -S ; else tmux swap-window -t -1 ; fi'
bind-key -n M-Right run-shell 'if tmux list-windows | tail -n 1 | grep -q active ; then tmux move-window -t 0 \; move-window -r \; refresh-client -S ; else tmux swap-window -t +1 ; fi'

This works by temporarily moving the last window to the unused index-0 and then calling move-window -r to renumber them starting from 1 again. It works similarly when moving the first window to the end; by picking a huge number you'll never use, it ensures that when move-window -r fires again everything will be numbered like you'd expect. If you're wondering about refresh-client -S, that's necessary because sometimes, while the reordering from move-window will work properly, the status bar won't update until further changes are made. By forcing a refresh of just the status bar (-S), you avoid this.

The only issue I can find with this approach is that swap-window will implicitly alter the last-used window to the one you swapped with. Thus, if you are on window #1, switch to window four and move it back one, you'll find that your last-used window is the new # 4 (formerly #3) instead of #1. There doesn't seem to be a way around this.

share|improve this answer

Most simple solution from man, is to use the default bindings:

{           Swap the current pane with the previous pane.
}           Swap the current pane with the next pane.
share|improve this answer
1  
tmux windows and panes are different. – Marcello Romani Feb 19 '15 at 10:00
1  
This still helped me get what I wanted – lkraav May 24 '15 at 12:00

I think you want to bind a new key combination to the 'choose-window' command.

I know you said you've already read the man page, but you should refer back to it. you need to modify your ~/.tmux.conf file to add a bind-key command.

Specifically, look at page 4 of the following.

tmux man page

share|improve this answer
    
choose-window doesn't seem to move the current window to a new position. – dan Feb 2 '11 at 20:20
    
Also, choose-window is already bound to C-B w. – kynan Jun 10 '13 at 18:10

This is the method I use. You still can't move a window to an occupied index, but you can move one to a higher (unused index) and rearrange in the gap where the previous index was.

Say you have 3 windows and want to add a fourth but in the place where 3 previously was.

Before you add a new window: Tmux prefix then . will open up the move command. Type in 4 and the index of 3 will now become 4, then simply add another window and it will be at index 3 and your old window will still be at index 4.

share|improve this answer
    
Although a helpful tip, this is not answering the question. – Jan Doggen Oct 31 '14 at 16:17

For those of you who use byobu as your wrapper for tmux, you can swap the current window with the previous or next window with:

Ctrl-Shift-F3
Ctrl-Shift-F4

The key binding defined by byobu for these keys may be of interest:

bind-key -n    C-S-F3 swap-window -t :-1
bind-key -n    C-S-F4 swap-window -t :+1
share|improve this answer

Since Ashish Ariga's answer doesn't work on version 1.9a and below. I use < and > to swap window to left and right, respectively, by adding the line below to .tmux.conf.

# swap window to left or right
bind-key -r < swap-window -t -1
bind-key -r > swap-window -t +1
share|improve this answer

First, open the tmux command press and release:

Ctrl + b 

And to change the actual window to the right window (in circular order), just do:

}

To change the actual window to left:

{
  • Don't forget to use SHIFT when press } or {.
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .