How to renumber windows in tmux?

Quick Answer

To renumber windows in tmux, use the move-window command with the -r flag to automatically renumber all windows sequentially. Run Ctrl+b : move-window -r to close gaps in window numbering. For automatic renumbering whenever windows are created or closed, add set -g renumber-windows on to your ~/.tmux.conf file.

# One-time renumbering
Ctrl+b :move-window -r

# Automatic renumbering (in ~/.tmux.conf)
set -g renumber-windows on

Detailed Explanation

Window numbering in tmux starts at index 0 (or 1 if configured) and increments for each new window. When you close windows, gaps can appear in the numbering sequence. Renumbering windows keeps your window indexes sequential, making navigation more intuitive.

Manual Renumbering

To renumber all windows on demand:

  1. Press Ctrl+b : to enter the tmux command prompt
  2. Type move-window -r and press Enter

This command will renumber all windows sequentially, closing any gaps in the numbering.

Automatic Renumbering

To enable automatic window renumbering (available in tmux 1.7 and later):

# Add to your ~/.tmux.conf
set -g renumber-windows on

With this option enabled, tmux will automatically renumber windows whenever a window is closed, ensuring that window numbers remain sequential at all times.

Moving Windows to Specific Numbers

You can also manually move windows to specific numbers:

# Move current window to position 3
Ctrl+b :move-window -t 3

# Swap window 2 with window 4
Ctrl+b :swap-window -s 2 -t 4

# Move window 2 to position 4, shifting other windows if needed
Ctrl+b :move-window -s 2 -t 4

The -s flag specifies the source window, and -t specifies the target position. If you omit -s, the current window is used as the source.

Starting Window Numbering from 1

By default, tmux starts window numbering at 0. Many users prefer to start at 1 for easier keyboard navigation (since the 1 key is more accessible than 0). To change this:

# Add to your ~/.tmux.conf
set -g base-index 1
setw -g pane-base-index 1

The first line sets window numbering to start at 1, and the second line does the same for panes within windows.

Custom Keybinding for Renumbering

To create a custom keybinding for renumbering windows:

# Add to your ~/.tmux.conf
bind R move-window -r \; display-message "Windows renumbered"

With this configuration, pressing Ctrl+b R will renumber your windows and display a confirmation message.

Window Navigation Tips

With properly numbered windows, you can use these navigation shortcuts:

  • Ctrl+b 0-9 - Jump directly to window 0-9
  • Ctrl+b n - Next window
  • Ctrl+b p - Previous window
  • Ctrl+b l - Last window (toggle between current and previous)
  • Ctrl+b w - Show window list for selection