30
votes

I have three windows:

1:zsh 2:vim* 3:htop

When I delete the current window (#2), I have these windows left:

1:zsh 3:htop

How can I make it so that it automatically renumbers them as

1:zsh 2:htop

If I recall correctly, this is the default behavior of GNU Screen. I know I could always :swap-window, but I would like to know if this is possible automatically.

4
This would be a better question for superuser.com or unix.stackexchange.com.ziesemer
Patches for a feature like this have been floated on the tmux-users mailing list, but they have not been incorporated into the main code. A quick search of “tmux renumber windows” turned up a script that does such renumbering, though you would have to manually invoke it (from a shell or through a binding via run-shell).Chris Johnsen
The recently released tmux 1.7 includes the renumber-windows session option (keeps window numbers gapless), and the move-window -r command (does a one-time renumbering of windows). If you want “gapless” numbers for all sessions, then you could put set -g renumber-windows on in your config file (once you have tmux 1.7).Chris Johnsen

4 Answers

35
votes

Let's do it more simply.

If you are using tmux below version 1.7, append next line to ~/.tmux.conf:

 bind-key C-s run "for i in $(tmux lsw|awk -F: '{print $1}'); do tmux movew -s \$i; done"

You could sort all windows, by typing PREFIX-KEY, then Ctrl + s.

Else, if you are using tmux version 1.7 or above, as already everybody says, append next line to ~/.tmux.conf:

 set-option -g renumber-windows on
30
votes

Since tmux 1.7, you can type just one command to do so:

tmux movew -r
3
votes

This has now been implemented in C and submitted to tmux CVS on OpenBSD. Will hit the sourceforge portable release soon.

https://github.com/ThomasAdam/tmux-obsd/commit/c42e9b038dcdd36944e76954258a484387bd988f

2
votes

The bash script below (updated version of [1] to reflect changes in tmux API) reorders tmux sessions. I suggest adding this as a bash function which you can call from any shell.

# re-number tmux sessions                                                                                                                                                                                                                 
for session in $(tmux ls | awk -F: '{print $1}') ;do                                                                                                                                                                                      
    inum=0                                                                                                                                                                                                                                
    for window in $(tmux lsw -t 0 | awk -F: '/^[0-9*]/ {print $1}') ;do                                                                                                                                                                   
        if [ ${window} -gt ${inum} ] ;then                                                                                                                                                                                                
            echo "${session}:${window} -> ${session}:${inum}"                                                                                                                                                                             
            tmux movew -d -s ${session}:${window} -t ${session}:${inum}                                                                                                                                                                   
        fi                                                                                                                                                                                                                                
        inum=$((${inum}+1))                                                                                                                                                                                                               
    done                                                                                                                                                                                                                                  
done

[1] http://brainscraps.wikidot.com/tmux-renum