621

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.

Andrew Marshall
  • 382
  • 1
  • 2
  • 15
dan
  • 7,227
  • 5
  • 23
  • 23
  • 153
    `C-b`, `.` lets you renumber a window. – isomorphismes Feb 19 '14 at 01:01
  • 9
    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
  • 1
    @nealmcb the easiest i could come up with is to set base-index to 1, renumber, then move your window into the empty 0 slot: http://superuser.com/a/1155999/674549 – eMBee Dec 13 '16 at 06:53
  • For difference of `swap-window -s number` and `swap-window -t number` to swap current window, see [my answer here](https://superuser.com/a/1257041/487198). – qeatzy Oct 07 '17 at 08:47
  • See [this answer](https://unix.stackexchange.com/a/396668/202329) for a bash function to shift range of tmux windows, eg, [2-5] of [0-6] windows. – qeatzy Oct 07 '17 at 13:03
  • 3
    @isomorphismes `C-b .` renumbers only to a non-existing window number. `:swap-window` can truly move two existing windows – user989762 Apr 19 '18 at 20:00
  • 2
    @nealmcb Maybe you like my method: https://superuser.com/a/1663968/121441 – gitaarik Jul 22 '21 at 01:37
  • Lots of fine answers here, but missing the ultimate answer for "how do I reorder when having windows 1, 2, 3, 6, 7 to 1-5". Tmux has `:move-window -r` to do this sorta thing – shmup Jul 26 '22 at 16:00

24 Answers24

708

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). Command move-window -t <NUMBER> is by default bound to Ctrl+B, ..

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
smbear
  • 206
  • 2
  • 8
matlehmann
  • 7,196
  • 1
  • 13
  • 4
  • 8
    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
  • 3
    You might want to have a look at `movew`. – Marcello Romani Feb 19 '15 at 09:57
  • 8
    Also, `bind-key L swap-window -t -1` to make it the last window. –  Mar 15 '17 at 14:49
  • For difference of `swapw -s number` and `swapw -t number` to swap current window, see [my answer here](https://superuser.com/a/1257041/487198). – qeatzy Oct 07 '17 at 08:45
  • Just in case, "`swap-pane`" for split panels inside a window. Their IDs should be visible when switching (i.e. `Shift + Right`). – Artfaith Sep 27 '21 at 13:02
382

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

For tmux 3.0 version, you should use following key bindings

bind-key -n C-S-Left swap-window -t -1\; select-window -t -1
bind-key -n C-S-Right swap-window -t +1\; select-window -t +1
speedan
  • 3
  • 2
Ashish Ariga
  • 3,821
  • 1
  • 12
  • 2
  • 14
    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
  • I came here looking for the quickest way to write these exact bindings. Thanks! – Keith Smiley Jan 06 '14 at 19:25
  • 1
    My inability to easily move tmux windows has been bugging me for a few weeks. This makes it so much easier to deal with the windows! Many thanks from this recent tmux convert. – calvinf Jan 21 '14 at 20:09
  • This is seriously the most useful shortcut in my repertoire, thank you for sharing that. – glitch May 17 '14 at 23:48
  • 14
    Great hint, prefer `bind-key S-Left swap-window -t -1`, so I can do shift+arrow, as I don't like messing up key binds for programs. – demure Jun 01 '14 at 00:35
  • This doesn't appear to work in tmux 1.9a. Can someone else confirm what version this key binding will work on? – Justin Garrison Jul 30 '14 at 22:47
  • 5
    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 – Rho Phi Aug 21 '15 at 08:09
  • 4
    I like the idea of binding key C-S-Left/Right. However, when I was trying this on my Mac(10.12.4 Sierra) with tmux 2.3, it's not working. I'm not sure if it's captured by the Terminal.app or other system settings. Any help would be appreciated! – Dreamer May 02 '17 at 00:26
  • excellent work! sorted my windows out finally ! I turned off numbers on windows – wuxmedia Feb 26 '18 at 08:41
  • what is "Gareth's answer?" – rubo77 Sep 16 '19 at 23:19
  • 10
    For tmux >= 3.0, you can use the -d flag. See https://github.com/tmux/tmux/issues/2056 – Andrei Cioara Feb 02 '20 at 03:43
  • 2
    @brianpeiris The "wrapping around the end/begining" bit is a bit wierd though: the previous first tab becomes the last tab! – drevicko Nov 17 '20 at 05:42
  • 1
    @demure prefer "<" and ">", `bind-key -r "<" swap-window -t -1\; select-window -t -1` – Qinsi Jan 07 '21 at 07:10
  • @Qinsi You don't need `select-window -t ...`, just `swap-window -d -t ...` to keep focus on the moving window. – Martin Braun Jun 23 '23 at 18:42
81

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

Randal Schwartz
  • 911
  • 6
  • 5
  • 12
    This is bound to C-b . – vedang Jun 30 '11 at 12:59
  • 1
    Thanks! This is much better than the accepted answer imo. – Asherah Feb 14 '12 at 22:27
  • 6
    @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
  • 6
    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
  • I was wondering if the number 42 is a reference to The Hitchhiker's Guide to the Galaxy? – omar Dec 12 '14 at 00:32
  • Omar - of course! It's one of my favorite numbers to use in examples. – Randal Schwartz Sep 21 '17 at 21:49
36

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

isomorphismes
  • 1,864
  • 2
  • 20
  • 31
14

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
haridsv
  • 649
  • 1
  • 8
  • 18
piec
  • 655
  • 5
  • 7
  • 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 09:33
12

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
qun
  • 451
  • 4
  • 4
9

tmux-pain-control provides ctrl-b > and ctrl-b < to move the focused window right and left, wrapping around.

oblitum
  • 241
  • 3
  • 9
9

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]

  • 2
    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
8

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.
jhvaras
  • 199
  • 1
  • 4
7

For sane and easy window swapping:

# Bind P and N (capitals) to moving the current window around.
bind-key N swap-window -t +1 \; next-window
bind-key P swap-window -t -1 \; previous-window

With \; you can combine two commands to one keybinding. So we first swap the window, then we move the focus to that (original) window.

So when you do prefix -> Shift + N, it moves the current window one forward, and keeps the focus on that window.

It puzzles me why there's not a direct command for this in tmux. Why would you want something more complicated? Anyway, this fixes. it.

gitaarik
  • 640
  • 8
  • 18
6

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
Dave Kuhlman
  • 61
  • 1
  • 2
6

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 -d -t -1'
bind-key -n M-Right run-shell 'tmux list-windows | tail -n 1 | grep -q active || tmux swap-window -d -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 -d -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 -d -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.

EDIT in 2020: Recent tmux versions require -d to keep the old behavior of moving the current window and staying with it in its new position.

Flowchartsman
  • 233
  • 2
  • 6
  • This works great, I had an external script that achieves the same thing in 20 lines of code, your bind-key calls are much simpler and more efficient. Just one note for 2020 - current tmux versions don't make the swapped window automatically selected, so after `swap-window -t -1;` I needed to add `tmux previous-window;` and after `swap-window -t +1;` I had to add `tmux next-window;` – Rafał G. Sep 10 '20 at 20:54
  • Thanks for the update, but this solution loops around when the windows hit the edge. It's better to use `swap-window -d` to get the old behavior, I will update my answer. – Flowchartsman Nov 30 '20 at 18:11
  • you are genius! that is what I was looking for – bora89 May 25 '23 at 10:30
4

I'm surprised that no one found this out, but after digging in the tmux man page, you can use

move-window -b -t <target-window>

to move to the current window to before the target window.

So to do exactly what the OP is asking, do this:

move-window -b -t 0
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 29 '23 at 20:51
  • I think this is the right way (upvoted). Regarding "I'm surprised that no one found this out": the question is from 2011, many answers are old; AFAIK the support for `-b` in `tmux move-window` was added in tmux 3.2 released Apr 13, 2021. – Kamil Maciorowski Jan 29 '23 at 21:04
3

you can use base-index to change the starting number for your windows. if you set base-index to 1, then move-window -r will renumber all windows starting at 1, thus freeing up 0 to move a window into its slot:

set base-index 1
move-window -r
move-window -t 0

when done, you could reset base-index to 0 if you want to use move-window -r later without moving your window at 0

eMBee
  • 141
  • 3
2

Both swap-window -s 5 and swap-window -t 5 swap current window with window 5, but with different semantics.

swap-window -s 5

  • window 5 program in effect with current win number.
  • recent window number remain unchanged.
  • repeat same command will roll back to previous state.

swap-window -t 5

  • current program in effect with number 5.
  • recent override to current window number.

the number in swap-window -s number and -t number could be both absolute, eg, 5, and relative, eg, -1, +2.

P.S. below is excerpt of tmux statusline illustrating effect of swap-window -s 5

recent:
[0] 0:vim- 1:mpl 2:py2* 3:numpy 4:plot 5:mc

now, after last-window:
[0] 0:vim* 1:mpl 2:py2- 3:numpy 4:plot 5:mc

after swapw -s 5:
[0] 0:mc* 1:mpl 2:py2- 3:numpy 4:plot 5:vim

another swapw -s 5:
[0] 0:vim* 1:mpl 2:py2- 3:numpy 4:plot 5:mc

another last-window:
[0] 0:vim- 1:mpl 2:py2* 3:numpy 4:plot 5:mc

qeatzy
  • 285
  • 2
  • 12
  • See [this answer](https://unix.stackexchange.com/a/396668/202329) for a bash function to shift range of tmux windows, eg, [2-5] of [0-6] windows. – qeatzy Oct 07 '17 at 13:08
2

From the tmux window, try the following to swap window 0 with 1:

$ tmux swap-window -d -s 0 -t 1

2

No need for the commandline (which is Ctrl + b and then :)

Just move your windows around with

Ctrl + b + :

then enter the new (free) window number and hit Enter

If there is no free window number, use Ctrl + b + . to renumber a window.

(Tip: name your windows with Ctrl + b + , if you loose track of which is which)

rubo77
  • 4,680
  • 11
  • 45
  • 79
2

I discovered the desired behaviour is possible with the following options set:

set -g base-index 1
set -g pane-base-index 1 # for consistency
set-option -g renumber-windows on
bind 0 move-window -t0

If base-index is 1, it allows position 0 to be assigned to still, and the renumbers, freeing up position 0 again. Now C-b 0 will work as does C-b . 0

jon
  • 21
  • 1
1

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 {.
1

For what it's worth:

I hacked this script together to be able to order all the windows in a "TUI". It lists all your windows in a temporary file, opens it with your default editor (assumes that you've set $EDITOR). After this you can reorder the lines and after you save and close the file, the windows are reordered accordingly. (This is similar to ordering commits when doing git rebase -i)

#!/bin/bash

# Usage: tmux-mv    
#   Move your tmux windows around in an editor

tmpfile=$(mktemp)
tmux list-windows > $tmpfile
$EDITOR $tmpfile

# Move all windows to 50..x in the order you just specified
# Assumes you don't have 50 windows already(!)
cat $tmpfile | awk -F ":" '{ print " -s " $1 " -t 5" NR-1 }' |\
  xargs -I {} sh -c 'tmux move-window -d {}'

# Move them back down, retaining the order
tmux move-window -d -r
rm $tmpfile

It could probably be improved upon a lot, in particular:

NOTE: You might be moved to another window after running the command.

A gif of it in action (github)

steinar
  • 111
  • 3
1

None of the answers here satisfied me, so I wrote a script that (sort-of) mimicks the screen window-moving behavior for tmux.

It detects whether the destination window number is smaller than the smallest window number or bigger than the biggest window number, and if so, nudges all the other windows to the right or left (respectively) and re-numbers them to from 1 - N incrementally. This is done with the move-window command.

If the window number is in-between the smallest/biggest numbers (i.e., presumably, matches an existing window number), it just swaps them with the swap-window command.

Here's the script:

#!/usr/bin/env bash
# Filename: ~/tmux-windowswap
# Function that provides improved window-swapping functionality for tmux
maxwin=$(tmux list-windows | cut -d: -f1 | sort -nr | head -n1)
minwin=$(tmux list-windows | cut -d: -f1 | sort -n | head -n1)

# Error checking
if [[ -z $2 ]]; then
    echo "Error: No window specified."
elif ! [[ $2 =~ ^-?[0-9]+$ ]]; then
    echo "Error: Bad window number specified."

# Bigger than everything; slide it to the far right, then renumber
elif [[ $2 -gt $maxwin ]]; then
    i=0
    tmux move-window -t:$((maxwin + 1))
    winlist=($(tmux list-windows | cut -d: -f1 | xargs))
    for n in ${winlist[@]}; do
      i=$((i + 1))
      tmux move-window -s:$n -t:$i
    done

# Smaller than everything; slide it to the far left, then renumber
elif [[ $2 -lt $minwin ]]; then
    tmux move-window -t:0
    winlist=($(tmux list-windows | cut -d: -f1 | xargs | rev))
    i=${#winlist[@]}  # start at highest indexed window
    for n in ${winlist[@]}; do
      tmux move-window -s:$n -t:$i
      i=$((i - 1))
    done

# In-between; just a simple swap
else
    tmux swap-window -t:$2
fi

Then, just add this simple key-binding to your .tmux.conf:

bind m command -p "Send window to:"  "run -b '~/tmux-windowswap #I %1'"

Note: To perfectly mimick screen window-swapping behavior, I believe when you move a window to an existing window number, it should take that window's place and all the other windows are nudged to the right. It would be fairly simple to modify this script to do so.

Luke Davis
  • 293
  • 3
  • 10
1

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

1

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.

mattLummus
  • 111
  • 4
0

This can also be done the following way:

Ctrl+M to mark the first window, and running :swap-windows from another window

This will swap them without having to know/remember the index numbers

Toto
  • 17,001
  • 56
  • 30
  • 41
Elvin
  • 21
  • 3