Skip to main content

Basic Series 4 - Mastering tmux for Efficient Terminal Management

· 5 min read

This guide explains how to use tmux (Terminal Multiplexer) to enhance your terminal productivity and manage multiple terminal sessions effectively.

What is tmux?

tmux is a terminal multiplexer that allows you to:

  • Create multiple terminal sessions within a single window
  • Detach from sessions without closing them
  • Reattach to sessions from different terminals
  • Split your terminal into multiple panes
  • Share terminal sessions with other users

Installation

[!NOTE] If you are using our servers, tmux is already installed.

# On Ubuntu/Debian
sudo apt-get install tmux

# On macOS with Homebrew
brew install tmux

# On Windows (via WSL)
sudo apt-get install tmux

Basic Usage

Starting tmux

# Start a new session
tmux

# Start a new named session
tmux new -s mysession

# List existing sessions
tmux ls

Essential Keyboard Shortcuts

All tmux commands start with a prefix key (default: Ctrl+b). Here are the most common commands:

Session Management

  • Ctrl+b then d - Detach from current session
  • Ctrl+b then $ - Rename current session
  • Ctrl+b then s - Switch between sessions

Window Management

  • Ctrl+b then c - Create new window
  • Ctrl+b then n - Next window
  • Ctrl+b then p - Previous window
  • Ctrl+b then & - Kill current window
  • Ctrl+b then , - Rename current window

Pane Management

  • Ctrl+b then % - Split pane vertically
  • Ctrl+b then " - Split pane horizontally
  • Ctrl+b then arrow - Move between panes
  • Ctrl+b then z - Zoom in/out of current pane
  • Ctrl+b then x - Kill current pane

Configuration

Create or edit ~/.tmux.conf:

# Enable mouse mode
set -g mouse on

# Start window numbering at 1
set -g base-index 1

# Start pane numbering at 1
setw -g pane-base-index 1

# Increase scrollback buffer size
set -g history-limit 50000

# Use vim keybindings in copy mode
setw -g mode-keys vi

# Update status bar every 15 seconds
set -g status-interval 15

# Enable focus events
set -g focus-events on

# Set terminal title
set -g set-titles on
set -g set-titles-string "#T"

# Status bar customization
set -g status-style bg=black,fg=white
set -g status-left "#[fg=green]#H #[fg=black]• #[fg=green]#(uname -r | cut -c 1-6)#[default]"
set -g status-right "#[fg=green]#(cut -d ' ' -f 1-3 /proc/loadavg)#[default]"

Common Use Cases

1. Remote Development

# Start a new session on remote server
ssh user@server
tmux new -s dev

# do somthing here ...

# Detach from session
Ctrl+b d

# Reattach to session
tmux attach -t dev

2. Multiple Terminal Windows

# Create new window
Ctrl+b c

# Switch between windows
Ctrl+b n # Next window
Ctrl+b p # Previous window

3. Split Screen Work

# Split screen vertically
Ctrl+b %

# Split screen horizontally
Ctrl+b "

# Move between panes
Ctrl+b arrow keys

Best Practices

  1. Session Naming: Always use descriptive names for your sessions
  2. Window Organization: Use windows for different projects or tasks
  3. Pane Layout: Use panes for related tasks within the same project
  4. Configuration: Customize your .tmux.conf to match your workflow
  5. Session Persistence: Use tmux-resurrect or tmux-continuum for session persistence

Advanced Features (Optional)

Session Persistence

Install tmux-resurrect:

# Install tmux plugin manager
git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm

# Add to .tmux.conf
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-resurrect'

# Install plugins
Ctrl+b I

Copy Mode

  1. Enter copy mode: Ctrl+b then [
  2. Use vim keys to navigate
  3. Space to start selection
  4. Enter to copy
  5. Ctrl+b then ] to paste

Synchronize Panes

# Toggle synchronize-panes
Ctrl+b :setw synchronize-panes on
Ctrl+b :setw synchronize-panes off

Troubleshooting

Common Issues

  1. Mouse Mode Not Working:

    • Ensure set -g mouse on is in your .tmux.conf
    • Restart tmux after configuration changes
  2. Copy/Paste Issues:

    • Use Ctrl+b then [ to enter copy mode
    • Use vim keys to navigate
    • Space to start selection
    • Enter to copy
    • Ctrl+b then ] to paste
  3. Session Recovery:

    • Use tmux attach to reattach to existing sessions
    • Use tmux ls to list available sessions

Additional Resources