38

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.

Joshua
  • 40,822
  • 8
  • 72
  • 132
Jimmy Zelinskie
  • 1,450
  • 2
  • 12
  • 12
  • This would be a better question for http://superuser.com or http://unix.stackexchange.com. – ziesemer Dec 12 '11 at 03:27
  • 2
    Patches for a feature like this have been [floated on the *tmux-users* mailing list](http://search.gmane.org/?query=renumber&group=gmane.comp.terminal-emulators.tmux.user), 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](http://brainscraps.wikidot.com/tmux-renum), though you would have to manually invoke it (from a shell or through a binding via `run-shell`). – Chris Johnsen Dec 13 '11 at 08:08
  • 5
    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 Oct 13 '12 at 22:02
  • Related: https://unix.stackexchange.com/questions/21742/renumbering-windows-in-tmux – Ciro Santilli OurBigBook.com Sep 24 '20 at 06:40

4 Answers4

45

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
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
displayname
  • 663
  • 8
  • 9
  • The bind-key option here does not work properly with tmux 1.6. The first window always gets renumbered to the end and the rest get moved down, so the windows behave like a circular list, rotating each time this is run and potentially reordering them completely. chris' answer below is much more reliable. – papercrane Jan 21 '16 at 20:26
  • It's worth to mention that the re-indexing currently happens on any window move "event". So, a window move may be required with a reloaded config while Tmux is running. – Artfaith Oct 07 '22 at 21:32
35

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

tmux movew -r
northteam
  • 469
  • 4
  • 7
3

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

ThomasAdam
  • 194
  • 2
2

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

chris
  • 7,222
  • 5
  • 31
  • 37