Quick Answer:
tmuxstarts a new session.Ctrl+bis the prefix key — press it first, then the command key.Ctrl+b ccreates a new window.Ctrl+b %splits vertically.Ctrl+b "splits horizontally.Ctrl+b ddetaches.tmux attachreattaches.
Why tmux?
tmux keeps your terminal sessions alive on the server even when you disconnect. SSH drops? Close your laptop? tmux sessions keep running. Reconnect and pick up exactly where you left off.
- Persistent sessions — survive SSH disconnects
- Multiple windows — like browser tabs for your terminal
- Split panes — see multiple terminals side by side
- Session sharing — multiple people can view the same session
Install
# Ubuntu/Debian
sudo apt install tmux -y
# CentOS/RHEL
sudo yum install tmux -y
# Mac
brew install tmux
# Check version
tmux -V
The Prefix Key
Every tmux command starts with the prefix key: Ctrl+b
Press Ctrl+b first, release it, then press the command key.
This guide writes it as Prefix + key. So Prefix + c means: press Ctrl+b, release, press c.
Sessions
A session is a collection of windows. You can have multiple sessions running simultaneously.
# Start new session
tmux
tmux new -s mysession # Named session
# Detach from session (keeps it running)
Prefix + d
# List sessions
tmux ls
# Attach to session
tmux attach # Last session
tmux attach -t mysession # By name
tmux a -t mysession # Short form
# Kill a session
tmux kill-session -t mysession
# Kill all sessions
tmux kill-server
# Rename current session
Prefix + $
# Switch between sessions
Prefix + s # Session picker
Prefix + ( # Previous session
Prefix + ) # Next session
Detach and Reattach (Most Common Use)
# Start a long-running task
tmux new -s deploy
./deploy-script.sh
# Detach (Ctrl+b, then d)
# Close SSH, go home, sleep...
# Reconnect later
ssh user@server
tmux attach -t deploy
# Your script is still running exactly where you left it
Windows
Windows are like tabs inside a session.
# Create new window
Prefix + c
# Switch windows
Prefix + 0 # Window 0
Prefix + 1 # Window 1
Prefix + n # Next window
Prefix + p # Previous window
Prefix + w # Window picker (list)
# Rename window
Prefix + ,
# Close window
Prefix + & # Or just type 'exit'
# Move window
Prefix + . # Move to a different index
Panes
Split a window into multiple terminal panes.
# Split vertically (left/right)
Prefix + %
# Split horizontally (top/bottom)
Prefix + "
# Navigate panes
Prefix + arrow keys # Move between panes
Prefix + o # Next pane
Prefix + ; # Last active pane
Prefix + q # Show pane numbers, press number to jump
# Resize panes
Prefix + Ctrl+arrow keys # Resize in arrow direction
Prefix + z # Toggle zoom (fullscreen current pane)
# Close pane
Prefix + x # Or just type 'exit'
# Swap panes
Prefix + { # Swap with previous
Prefix + } # Swap with next
# Convert pane to window
Prefix + !
# Layouts
Prefix + Space # Cycle through layouts
Prefix + Alt+1 # Even horizontal
Prefix + Alt+2 # Even vertical
Prefix + Alt+3 # Main horizontal
Prefix + Alt+4 # Main vertical
Prefix + Alt+5 # Tiled
Copy Mode
Scroll through output and copy text.
# Enter copy mode
Prefix + [
# In copy mode (emacs keys by default, vi if mode-keys is set):
# Navigate: h j k l (or arrow keys)
# Page up: Ctrl+u
# Page down: Ctrl+d
# Search forward: /
# Search backward: ?
# Start selection: Space
# Copy selection: Enter
# Quit copy mode: q
# Paste
Prefix + ]
Enable Mouse Support
Add to ~/.tmux.conf:
set -g mouse on
This lets you:
- Click to select panes
- Scroll with mouse wheel
- Resize panes by dragging borders
- Select text (hold Shift to select and copy normally)
Configuration (~/.tmux.conf)
Recommended Starter Config
cat > ~/.tmux.conf << 'EOF'
# Better prefix (Ctrl+a instead of Ctrl+b)
# unbind C-b
# set -g prefix C-a
# bind C-a send-prefix
# Start window numbering at 1
set -g base-index 1
setw -g pane-base-index 1
# Enable mouse
set -g mouse on
# More history
set -g history-limit 50000
# Faster key repetition
set -s escape-time 0
# Better split keys
bind | split-window -h -c "#{pane_current_path}"
bind - split-window -v -c "#{pane_current_path}"
# Reload config
bind r source-file ~/.tmux.conf \; display "Config reloaded!"
# Don't rename windows automatically
set -g allow-rename off
# Status bar
set -g status-style 'bg=#1a1b26 fg=#a9b1d6'
set -g status-left '#[fg=#7aa2f7,bold] #S '
set -g status-right '#[fg=#a9b1d6] %H:%M '
set -g window-status-current-style 'fg=#7aa2f7,bold'
# Vi mode for copy
setw -g mode-keys vi
EOF
Reload after editing:
tmux source-file ~/.tmux.conf
# Or from inside tmux: Prefix + : then type: source-file ~/.tmux.conf
Command Mode
Press Prefix + : to enter command mode (like vim's : prompt).
# Useful commands
:new-session -s name # Create new session
:rename-session name # Rename session
:rename-window name # Rename window
:split-window -h # Split vertically
:split-window -v # Split horizontally
:resize-pane -D 10 # Resize down 10 lines
:resize-pane -U 10 # Resize up
:resize-pane -L 10 # Resize left
:resize-pane -R 10 # Resize right
:swap-window -t 0 # Swap current window with window 0
:setw synchronize-panes on # Type in all panes simultaneously
:setw synchronize-panes off # Turn off sync
Common Workflows
Long-Running Server Task
tmux new -s backup
./run-backup.sh
# Prefix + d to detach
# Come back later: tmux attach -t backup
Monitoring Setup
tmux new -s monitor
# Split into 4 panes
Prefix + % # Split vertical
Prefix + " # Split horizontal (left pane)
Prefix + o # Move to right pane
Prefix + " # Split horizontal (right pane)
# Pane 1: htop
# Pane 2: tail -f /var/log/nginx/access.log
# Pane 3: docker stats
# Pane 4: watch df -h
Development Setup
tmux new -s dev
# Window 1: code editor (vim)
Prefix + c
# Window 2: server (npm start)
Prefix + c
# Window 3: git/shell
Send Command to All Panes
# Enable synchronized panes
Prefix + : then type: setw synchronize-panes on
# Now everything you type goes to ALL panes
# Great for running the same command on multiple servers
# Turn off
Prefix + : then type: setw synchronize-panes off
Quick Reference Card
Sessions
| Key | Action |
|---|---|
tmux new -s name |
New named session |
tmux attach -t name |
Attach to session |
tmux ls |
List sessions |
Prefix + d |
Detach |
Prefix + s |
Session picker |
Prefix + $ |
Rename session |
Windows
| Key | Action |
|---|---|
Prefix + c |
New window |
Prefix + n |
Next window |
Prefix + p |
Previous window |
Prefix + 0-9 |
Go to window N |
Prefix + w |
Window picker |
Prefix + , |
Rename window |
Prefix + & |
Kill window |
Panes
| Key | Action |
|---|---|
Prefix + % |
Split vertical |
Prefix + " |
Split horizontal |
Prefix + arrows |
Navigate panes |
Prefix + z |
Zoom/unzoom pane |
Prefix + x |
Kill pane |
Prefix + Space |
Cycle layouts |
Prefix + q |
Show pane numbers |
Prefix + ! |
Pane → window |
Copy Mode
| Key | Action |
|---|---|
Prefix + [ |
Enter copy mode |
Space |
Start selection |
Enter |
Copy and exit |
Prefix + ] |
Paste |
q |
Exit copy mode |
tmux vs screen
| tmux | screen | |
|---|---|---|
| Status bar | Built-in, configurable | Basic |
| Pane splitting | Native, easy | Possible but clunky |
| Scripting | Powerful command API | Limited |
| Mouse support | Good | Limited |
| Active development | Yes | Maintenance mode |
| Default on most distros | Sometimes | Usually |
tmux is the modern choice. Use screen only if tmux is not available.