Renumbering windows in tmux

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?

Asked By: dogbane

||

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.

Answered By: gelraen

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
    
Answered By: Chris Johnsen

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..."
Answered By: gospes

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
Answered By: Pistos

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
Answered By: jojman
Categories: Answers Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.