Tmux – Terminal Multiplexer for Efficient Command Line Workflows

4.9 Stars
Version 3.4
1 MB

Mastering Tmux: The Ultimate Terminal Multiplexer Guide

Tmux stands as one of the most transformative tools for command-line users, enabling management of multiple terminal sessions within a single window. As a terminal multiplexer, tmux allows splitting your terminal into panes, creating multiple windows, and maintaining persistent sessions that survive disconnection. For developers, system administrators, and power users who spend significant time in the terminal, tmux fundamentally enhances productivity by organizing workflows into logical, manageable units.

The power of tmux extends beyond simple window management. Sessions persist independently of the terminal that created them, meaning you can disconnect from a remote server while processes continue running, then reconnect later and resume exactly where you left off. This capability proves invaluable for long-running tasks, pair programming sessions, and maintaining consistent development environments across different machines.

Core Concepts: Sessions, Windows, and Panes

Understanding tmux’s hierarchical structure is essential for effective use. Sessions represent the top-level container, containing one or more windows. Each window occupies the full terminal screen and can contain one or more panes. Panes divide a window into rectangular sections, each running its own shell or program.

This hierarchy enables sophisticated workspace organization. A typical development session might include a window for editing code with multiple panes showing different files, another window for running tests, and a third for monitoring logs. Switching between contexts becomes instantaneous, and the entire setup persists across terminal connections.

Installing Tmux

Tmux is available in the repositories of virtually every Unix-like operating system.

Linux Installation

# Ubuntu/Debian
sudo apt update
sudo apt install tmux

# Fedora
sudo dnf install tmux

# Arch Linux
sudo pacman -S tmux

# CentOS/RHEL
sudo yum install tmux

# openSUSE
sudo zypper install tmux

# Alpine
sudo apk add tmux

# Build from source (latest version)
git clone https://github.com/tmux/tmux.git
cd tmux
sh autogen.sh
./configure && make
sudo make install

# Verify installation
tmux -V

macOS Installation

# Homebrew
brew install tmux

# MacPorts
sudo port install tmux

# Verify
tmux -V

Basic Tmux Usage

Tmux commands begin with a prefix key (default: Ctrl+b), followed by a command key. Understanding this pattern unlocks all tmux functionality.

# Start new session
tmux
tmux new
tmux new -s session-name

# Detach from session
Ctrl+b d

# List sessions
tmux ls
tmux list-sessions

# Attach to session
tmux attach
tmux attach -t session-name
tmux a -t session-name

# Kill session
tmux kill-session -t session-name
tmux kill-server          # Kill all sessions

# Rename session
Ctrl+b $

# Within tmux, prefix commands with Ctrl+b
# Notation: Ctrl+b then x = C-b x

# Session commands
C-b d                     # Detach
C-b $                     # Rename session
C-b s                     # List sessions
C-b (                     # Previous session
C-b )                     # Next session

Window Management

Windows provide full-screen workspaces within a session, each with its own set of panes.

# Create new window
C-b c                     # Create window
C-b ,                     # Rename window

# Navigate windows
C-b n                     # Next window
C-b p                     # Previous window
C-b 0-9                   # Go to window number
C-b w                     # List windows
C-b f                     # Find window by name
C-b '                     # Go to window by index

# Window operations
C-b &                     # Kill window
C-b .                     # Move window

# Command line window creation
tmux new-window
tmux new-window -n window-name
tmux new-window "htop"    # Start with command

# Swap windows
C-b :swap-window -t 0     # Swap with window 0

# Move window
tmux move-window -t 3     # Move to index 3

Pane Management

Panes divide windows into multiple terminal instances, enabling side-by-side work.

# Split panes
C-b %                     # Vertical split
C-b "                     # Horizontal split

# Navigate panes
C-b Arrow                 # Move to pane in direction
C-b o                     # Next pane
C-b ;                     # Last active pane
C-b q                     # Show pane numbers
C-b q 0-9                 # Go to pane number

# Resize panes
C-b C-Arrow               # Resize in direction
C-b :resize-pane -D 10    # Resize down 10 lines
C-b :resize-pane -U 10    # Resize up
C-b :resize-pane -L 10    # Resize left
C-b :resize-pane -R 10    # Resize right
C-b z                     # Toggle pane zoom (fullscreen)

# Pane operations
C-b x                     # Kill pane
C-b !                     # Convert pane to window
C-b {                     # Swap with previous pane
C-b }                     # Swap with next pane
C-b Space                 # Cycle layouts

# Layouts
C-b M-1                   # Even horizontal
C-b M-2                   # Even vertical
C-b M-3                   # Main horizontal
C-b M-4                   # Main vertical
C-b M-5                   # Tiled

# Move pane to another window
C-b :join-pane -t :1      # Move to window 1
C-b :break-pane           # Pane to new window

Copy Mode and Scrollback

Copy mode enables scrolling through terminal output and copying text.

# Enter copy mode
C-b [

# Copy mode navigation (vi mode)
# First, add to .tmux.conf:
# set-window-option -g mode-keys vi

# In copy mode:
hjkl                      # Navigate
Ctrl+u                    # Page up
Ctrl+d                    # Page down
g                         # Top
G                         # Bottom
/                         # Search forward
?                         # Search backward
n                         # Next search result
N                         # Previous result

# Selection and copying
Space                     # Start selection
Enter                     # Copy selection and exit
v                         # Toggle selection mode
V                         # Line selection

# Paste
C-b ]                     # Paste buffer

# Buffer operations
C-b =                     # Choose buffer to paste
C-b :list-buffers         # List all buffers
C-b :save-buffer file.txt # Save buffer to file
C-b :load-buffer file.txt # Load file to buffer

# Copy to system clipboard (requires configuration)
# Add to .tmux.conf:
bind -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "xclip -selection clipboard"

Configuration File

The ~/.tmux.conf file customizes tmux behavior. Changes take effect on restart or after sourcing.

# ~/.tmux.conf

# Change prefix to Ctrl+a
unbind C-b
set -g prefix C-a
bind C-a send-prefix

# Enable mouse support
set -g mouse on

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

# Renumber windows when one is closed
set -g renumber-windows on

# Increase scrollback buffer
set -g history-limit 50000

# Faster escape time
set -sg escape-time 0

# Vi mode for copy
setw -g mode-keys vi

# Vi-style pane navigation
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R

# Easy pane resizing
bind -r H resize-pane -L 5
bind -r J resize-pane -D 5
bind -r K resize-pane -U 5
bind -r L resize-pane -R 5

# Split panes with | and -
bind | split-window -h -c "#{pane_current_path}"
bind - split-window -v -c "#{pane_current_path}"

# New window in current path
bind c new-window -c "#{pane_current_path}"

# Reload configuration
bind r source-file ~/.tmux.conf \; display "Config reloaded!"

# Enable true color
set -g default-terminal "tmux-256color"
set -ga terminal-overrides ",*256col*:Tc"

# Status bar configuration
set -g status-position top
set -g status-style bg=colour235,fg=colour136
set -g status-left '#[fg=colour39]#S '
set -g status-right '#[fg=colour39]%Y-%m-%d %H:%M'
set -g status-left-length 40

# Window status
setw -g window-status-current-style fg=colour166,bold
setw -g window-status-style fg=colour244

# Pane borders
set -g pane-border-style fg=colour235
set -g pane-active-border-style fg=colour39

# Message styling
set -g message-style bg=colour235,fg=colour166

# Apply configuration
# From within tmux:
C-b :source-file ~/.tmux.conf
# Or restart tmux

Session Management Scripts

Automating session creation enables consistent, reproducible workspaces.

#!/bin/bash
# dev-session.sh - Create development environment

SESSION="dev"

# Check if session exists
tmux has-session -t $SESSION 2>/dev/null

if [ $? != 0 ]; then
    # Create new session
    tmux new-session -d -s $SESSION -n editor
    
    # Editor window with splits
    tmux send-keys -t $SESSION:editor "nvim ." C-m
    
    # Terminal window
    tmux new-window -t $SESSION -n terminal
    tmux send-keys -t $SESSION:terminal "cd ~/projects" C-m
    
    # Server window with splits
    tmux new-window -t $SESSION -n server
    tmux split-window -h -t $SESSION:server
    tmux send-keys -t $SESSION:server.0 "npm run dev" C-m
    tmux send-keys -t $SESSION:server.1 "docker-compose logs -f" C-m
    
    # Git window
    tmux new-window -t $SESSION -n git
    tmux send-keys -t $SESSION:git "lazygit" C-m
    
    # Monitoring window
    tmux new-window -t $SESSION -n monitor
    tmux split-window -v -t $SESSION:monitor
    tmux send-keys -t $SESSION:monitor.0 "htop" C-m
    tmux send-keys -t $SESSION:monitor.1 "watch -n 1 df -h" C-m
    
    # Select first window
    tmux select-window -t $SESSION:editor
fi

# Attach to session
tmux attach -t $SESSION

# Run script
chmod +x dev-session.sh
./dev-session.sh

Tmux Plugin Manager (TPM)

TPM enables installing and managing tmux plugins easily.

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

# Add to ~/.tmux.conf
# List of plugins
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'
set -g @plugin 'tmux-plugins/tmux-resurrect'
set -g @plugin 'tmux-plugins/tmux-continuum'
set -g @plugin 'tmux-plugins/tmux-yank'
set -g @plugin 'catppuccin/tmux'

# Initialize TPM (keep at bottom)
run '~/.tmux/plugins/tpm/tpm'

# Install plugins
C-b I                     # Install plugins

# Update plugins
C-b U                     # Update plugins

# Popular plugins:
# tmux-resurrect: Save/restore sessions
# tmux-continuum: Auto save/restore
# tmux-yank: Copy to system clipboard
# tmux-sensible: Sensible defaults
# tmux-prefix-highlight: Show when prefix is pressed

# Configure tmux-resurrect
set -g @resurrect-capture-pane-contents 'on'
set -g @resurrect-strategy-nvim 'session'

# Save: C-b C-s
# Restore: C-b C-r

# Configure tmux-continuum
set -g @continuum-restore 'on'
set -g @continuum-save-interval '15'

Advanced Features

Power users leverage tmux’s advanced capabilities for sophisticated workflows.

# Synchronize panes (type in all panes simultaneously)
C-b :setw synchronize-panes on
C-b :setw synchronize-panes off

# Save pane content to file
C-b :capture-pane -S -3000
C-b :save-buffer ~/pane-output.txt

# Pipe pane output to file
C-b :pipe-pane -o 'cat >> ~/output.log'
C-b :pipe-pane                    # Stop piping

# Link window from another session
C-b :link-window -s source:1 -t target:2

# Monitor activity/silence
C-b :setw monitor-activity on
C-b :setw monitor-silence 30

# Lock session
C-b :lock-session

# Custom key tables
# Add to .tmux.conf:
bind -T custom h select-pane -L
bind -T custom j select-pane -D
bind Enter switch-client -T custom

# Hooks
set-hook -g after-new-window 'command'
set-hook -g session-created 'command'

# Display popup (tmux 3.2+)
C-b :display-popup
C-b :display-popup -E "htop"

# Run command in all panes
# Add to .tmux.conf:
bind E command-prompt -p "Command:" \
    "run \"tmux list-panes -a -F '##{session_name}:##{window_index}.##{pane_index}' | \
    xargs -I {} tmux send-keys -t {} '%1' Enter\""

Remote Server Usage

Tmux excels at maintaining persistent remote sessions.

# SSH to server and start/attach tmux
ssh user@server -t "tmux attach || tmux new"

# Named session on server
ssh user@server -t "tmux attach -t work || tmux new -s work"

# Attach to shared session (pair programming)
# User 1 creates:
tmux -S /tmp/shared new -s pair
chmod 777 /tmp/shared

# User 2 attaches:
tmux -S /tmp/shared attach -t pair

# Read-only attachment
tmux attach -t session -r

# Detach other clients
tmux attach -d -t session

Troubleshooting

Common issues and their solutions ensure smooth tmux operation.

# Colors not displaying correctly
# Add to .tmux.conf:
set -g default-terminal "tmux-256color"
# Or start tmux with:
tmux -2

# Mouse not working
set -g mouse on

# Clipboard not working
# Linux: Install xclip or xsel
sudo apt install xclip
# Add to .tmux.conf:
bind -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "xclip -selection clipboard"

# Slow escape key
set -sg escape-time 0

# Check tmux options
tmux show-options -g
tmux show-options -w

# Kill stuck sessions
tmux kill-server

# Check version
tmux -V

Conclusion

Tmux transforms terminal usage from managing individual commands to orchestrating comprehensive workspaces. Its session persistence, flexible window management, and extensive customization options make it indispensable for serious command-line users. Whether maintaining long-running processes on remote servers, organizing complex development environments, or simply keeping multiple projects accessible, tmux provides the tools for efficient terminal-based workflows.

Developer: Tmux Project

Download Options

Download Tmux – Terminal Multiplexer for Efficient Command Line Workflows

Version 3.4

File Size: 1 MB

Download Now
Safe & Secure

Verified and scanned for viruses

Regular Updates

Always get the latest version

24/7 Support

Help available when you need it