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?

share|improve this question
up vote 68 down vote accepted

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.

share|improve this answer
3  
Thank you for the movew command! – polym Feb 28 '15 at 15:50

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
    
share|improve this answer
2  
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 8:38

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). ps: i'm currently running tmux 1.9a.

EDIT:

The above can be replaced with the much simpler:

bind R                                      \
    move-window -r\;                        \
    display-message "Windows reordered..."
share|improve this answer
    
Is this different from running "move-window -r"? – PonyEars Aug 23 '14 at 20:43
2  
No it is not. I just didn't know about the -r option to move-window :) never too late to learn, thx! – gospes Aug 25 '14 at 8:07

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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