Renumbering tmux Windows

Mbm329 04:40, March 12, 2012 (UTC)

As of TMUX v1.6, this has been incorporated as a configuration option into the tmux code itself. renumber-windows [on | off] If on, when a window is closed in a session, automatically renumber the other windows in numerical order. This respects the base-index option if it has been set. If off, do not renumber the windows.

Occassionally your windows will become larger than 9 and when you remove some windows in the middle like windows 2 or 5 or the like, you might want to re-number them. Here's a small script I call "tmux_renum". By running it from any window in any session, it will renumber all the windows in all sessions from 0.

for session in $(tmux ls | awk -F: '{print $1}') ;do active_window=$(tmux lsw -t ${session} | awk -F: '/\(active\)$/ {print $1}') inum=0 for window in $(tmux lsw -t ${session} | awk -F: '{print $1}') ;do if [ ${window} -gt ${inum} ] ;then echo "${session}:${window} -> ${session}:${inum}" tmux movew -d -s ${session}:${window} -t ${session}:${inum} fi   if [ ${window} = ${active_window} ] ;then new_active_window=${inum} fi   inum=$((${inum}+1)) done tmux select-window -t ${session}:${new_active_window} done

You can even setup a keybinding for it in your tmux config. In the ~/.tmux.conf, I removed the "r" keybinding and replaced it with:

unbind r bind r run "~/bin/tmux_renum"
 * 1) r = tmux renumbering script

Thanks to Gerald Young for inspiration.