How to switch between tmux sessions?

Quick Answer

To switch between tmux sessions, press Ctrl+b s to open an interactive session list, then select a session using arrow keys and Enter. Alternatively, use Ctrl+b ( or Ctrl+b ) to cycle through sessions, or switch directly with tmux switch -t session_name.

Ctrl+b s

Detailed Explanation

Tmux allows you to create multiple sessions, each with its own set of windows and panes. As your workflow grows, you'll often need to switch between these sessions efficiently. Below are several methods to navigate between tmux sessions.

Method 1: Interactive session selector

# Press prefix followed by s
Ctrl+b s

This opens a menu showing all available sessions with their windows and panes. Navigate with arrow keys, then press Enter to select. This is the most user-friendly method, especially when you have many sessions with descriptive names.

Method 2: Cycling through sessions

# Move to previous session
Ctrl+b (

# Move to next session
Ctrl+b )

These key bindings cycle through sessions in order. This method is quick when you have only a few sessions and want to check each one sequentially.

Method 3: Switching by session name or index

# Using tmux command mode (within tmux)
Ctrl+b :switch-client -t session_name

# Direct command (within tmux)
Ctrl+b :attach -t session_name

# From the terminal (outside tmux)
tmux switch -t session_name
tmux attach -t session_name

This method is precise when you know exactly which session you need. Replace session_name with either the name of your session or its index number (starting from 0).

Method 4: Using session numbers with prefix key

# Add this to your ~/.tmux.conf
bind-key 0 switch-client -t 0
bind-key 1 switch-client -t 1
bind-key 2 switch-client -t 2
bind-key 3 switch-client -t 3
bind-key 4 switch-client -t 4
bind-key 5 switch-client -t 5
bind-key 6 switch-client -t 6
bind-key 7 switch-client -t 7
bind-key 8 switch-client -t 8
bind-key 9 switch-client -t 9

With this configuration, you can directly switch to a session by pressing Ctrl+b followed by the session number (0-9). This is extremely fast when you remember your session numbers.

Method 5: Last session toggle

# Add this to your ~/.tmux.conf
bind-key L switch-client -l

After adding this to your configuration, pressing Ctrl+b L will switch between the current and previously used session, similar to Alt+Tab in many GUI environments.

Method 6: Using session numbers

# List sessions with their numbers
tmux ls

# Switch by index (from outside tmux)
tmux attach -t 0

# Switch by index (from inside tmux)
Ctrl+b :switch-client -t 0

Each tmux session has an index number that you can use as an alternative to its name. This is useful for quick switching when session names are long or complex.

Pro Tip

Create a tmux status line indicator showing the number of available sessions:

# Add to your ~/.tmux.conf
set -g status-right '#[fg=green,bg=default]#(tmux list-sessions | wc -l) sessions'

This helps you keep track of how many sessions you have running at a glance.

Session Management Workflow

For efficient session navigation, consider these practices:

  • Use descriptive session names based on projects or tasks
  • Limit the number of active sessions to avoid navigation overhead
  • Use different status bar colors for different types of sessions
  • Create custom key bindings for frequently accessed sessions

Add this to your ~/.tmux.conf to change the status bar color based on session name:

set-hook -g session-created 'if-shell "echo #{session_name} | grep -q \"^dev-\"" "set -g status-bg colour22"'