190

I frequently end up with more than ten windows in tmux. Later on, I close some of my older ones. Is there a way to renumber, say window 15, to window 3 (which doesn't exist anymore)? Or to pack them all up again, so that there are no empty slots? I'd like to do this because it is difficult to jump to higher numbered windows, because you can't do Ctrl+B, 15. I have to use Ctrl+B, w to list the windows and then type the letter corresponding to the window I want to open.

I know that I can swap windows. For example, I could create a new window (Ctrl+B, c) which would open in the empty slot 3. I can then swapw window 15 and window 3 and then close window 15. Obviously, this is a tedious approach.

How do you manage many windows in tmux?

dogbane
  • 29,677

5 Answers5

202

Looks like you need this:

move-window [-rdk] [-s src-window] [-t dst-window]
              (alias: movew)
        This is similar to link-window, except the window at src-window
        is moved to dst-window.  With -r, all windows in the session are
        renumbered in sequential order, respecting the base-index option.

Calling movew without parameters moves current window to first free position. movew -r will renumber all the windows at once.

gelraen
  • 6,737
  • 35
    For those who need a more explicit instruction: ctrl b : then enter move-window -r (where ctrl b is the default prefix key) – mark Jan 16 '19 at 22:32
  • 4
    BTW, from within the tmux session, you can execute a tmux command in any window/shell and it'll do the operation on the containing tmux session. So a simple tmux move-window -r would do the same thing without the need to use the tmux prefox key combination. – Gurjeet Singh May 15 '20 at 00:11
142

tmux 1.7 has a couple of features that can help establish and maintain gapless/packed window numbers:

  • The move-window command learned a new -r option that will renumber all the windows in a session (either the current session, or one specified with the -t option).

    If you have a set of windows like { 1:A, 4:B, 15:C }, then you can run move-window -r to renumber them to { 1:A, 2:B, 3:C } (they will start with your base-index; 1 in this example).

  • When the renumber-windows session option is enabled, tmux will automatically renumber the windows of a session (as with move-window -r) after any window is closed.

    If you like this behavior, you can turn it on in the global value so that all sessions that to not override it will automatically have it enabled:

    set-option -g renumber-windows on
    
Chris Johnsen
  • 20,101
  • 5
    Finally! I've been searching for this renumber-windows option for ages! Always bugged me that new windows open on the "left" because I close some before. – Ory Band Nov 16 '15 at 08:38
  • For some odd reason renumber-windows does not update the window numbers, why might this be? – unrealapex Jan 17 '24 at 23:43
22

I often find myself in a situation where I have gaps in between window numbers, for example a session with windows:

1 3 4 8 9 13

I wrote a tmux script to reorder them without changing their respective order nor activating the 'renumbering-windows' option. The result:

1 2 3 4 5 6

Put the following in your .tmux.conf:

bind R                                      \
    set -g renumber-windows on\;            \
    new-window\; kill-window\;              \
    set -g renumber-windows off\;           \
    display-message "Windows reordered..."

Hit [PREFIX]-R to reorder windows (or change the binding).

I'm currently running tmux 1.9a.

The above can be replaced with the much simpler:

bind R                                      \
    move-window -r\;                        \
    display-message "Windows reordered..."
Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
gospes
  • 404
20

If you just need windows to always renumber when one is killed, you can use this in .tmux.conf:

set-option -g renumber-windows on

Don't forget to reload the config with

tmux source ~/.tmux.conf          # default config file
abu_bua
  • 249
jojman
  • 597
13

The previous answers are all fine, but here's a concise solution if all you want is to shuffle a small number of windows now and again.

move-window -t <number>

will move the current window to a new window number (which must not already be occupied). To swap two windows, you temporarily move one window to a new number first. e.g. to swap windows 3 and 5:

move-window -s 3 -t 99
move-window -s 5 -t 3
move-window -s 99 -t 5
Pistos
  • 259