SSH connections drop. Scripts need to run overnight. You need six terminals for one task. tmux solves all of this.

Why tmux?

  1. Persistence: Sessions survive disconnects
  2. Multiplexing: Multiple windows and panes in one connection
  3. Sharing: Pair programming on the same session
  4. Automation: Scriptable terminal control

Getting Started

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# Start a new session
tmux

# Start named session
tmux new -s work

# Detach (inside tmux)
Ctrl+b d

# List sessions
tmux ls

# Reattach
tmux attach -t work

The prefix key is Ctrl+b by default. Press it, then the command key.

Essential Commands

KeysAction
Ctrl+b dDetach
Ctrl+b cNew window
Ctrl+b nNext window
Ctrl+b pPrevious window
Ctrl+b 0-9Jump to window
Ctrl+b %Split vertical
Ctrl+b "Split horizontal
Ctrl+b arrowMove between panes
Ctrl+b zZoom pane (toggle)
Ctrl+b [Scroll mode
Ctrl+b ?Help

Window Management

1
2
3
4
5
# Inside tmux
Ctrl+b c        # Create window
Ctrl+b ,        # Rename window
Ctrl+b w        # Window list (interactive)
Ctrl+b &        # Kill window

Organize by task:

WWWWiiiinnnnddddoooowwww0123::::cssloehodregevlselr---vdgtieeamni/elesrdea-irlftvoecvrroamrlmioaognusdsslogs

Pane Layouts

1
2
3
4
5
6
Ctrl+b %        # Split right
Ctrl+b "        # Split below
Ctrl+b {        # Swap pane left
Ctrl+b }        # Swap pane right
Ctrl+b space    # Cycle layouts
Ctrl+b x        # Kill pane

Resize panes:

1
2
3
4
Ctrl+b :resize-pane -D 10    # Down 10 lines
Ctrl+b :resize-pane -U 10    # Up
Ctrl+b :resize-pane -L 10    # Left
Ctrl+b :resize-pane -R 10    # Right

Or hold Ctrl+b and press arrow keys.

Configuration (~/.tmux.conf)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# Better prefix
unbind C-b
set -g prefix C-a
bind C-a send-prefix

# Start windows at 1, not 0
set -g base-index 1
setw -g pane-base-index 1

# Renumber windows when one closes
set -g renumber-windows on

# Longer history
set -g history-limit 50000

# Mouse support
set -g mouse on

# Faster escape (for vim)
set -sg escape-time 0

# Better splits (current directory)
bind | split-window -h -c "#{pane_current_path}"
bind - split-window -v -c "#{pane_current_path}"

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

# Easy reload
bind r source-file ~/.tmux.conf \; display "Reloaded!"

Reload config: Ctrl+b :source-file ~/.tmux.conf

Long-Running Tasks

The primary use case - tasks that outlive your connection:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Start session for a big job
tmux new -s backup

# Run the command
rsync -avz /data remote:/backup/

# Detach
Ctrl+b d

# Come back tomorrow
tmux attach -t backup

Even if your laptop sleeps or VPN drops, the job continues.

Scripted Sessions

Create a development environment automatically:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/bin/bash
# dev-session.sh

SESSION="dev"

# Kill existing session
tmux kill-session -t $SESSION 2>/dev/null

# Create new session with first window
tmux new-session -d -s $SESSION -n 'editor'

# Set up editor window
tmux send-keys -t $SESSION:editor 'cd ~/project && vim .' Enter

# Create server window
tmux new-window -t $SESSION -n 'server'
tmux send-keys -t $SESSION:server 'cd ~/project && npm run dev' Enter

# Create shell window
tmux new-window -t $SESSION -n 'shell'
tmux send-keys -t $SESSION:shell 'cd ~/project' Enter

# Create split pane for logs
tmux split-window -t $SESSION:shell -v
tmux send-keys -t $SESSION:shell.1 'tail -f /var/log/app.log' Enter

# Attach to session
tmux attach -t $SESSION

Session Persistence with tmux-resurrect

Install tmux plugins for session persistence across reboots:

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

Add to ~/.tmux.conf:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# Plugins
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-resurrect'
set -g @plugin 'tmux-plugins/tmux-continuum'

# Auto-save every 15 minutes
set -g @continuum-save-interval '15'

# Auto-restore on tmux start
set -g @continuum-restore 'on'

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

Save session: Ctrl+b Ctrl+s Restore session: Ctrl+b Ctrl+r

Pair Programming

Share a tmux session with a colleague:

1
2
3
4
5
6
7
# Create shared session
tmux new -s pairing

# Colleague attaches (same server)
tmux attach -t pairing

# Both see the same thing, can type

For read-only observers:

1
tmux attach -t pairing -r

Copy Mode

Navigate and copy text:

1
2
3
4
5
Ctrl+b [        # Enter copy mode
q               # Exit copy mode
Space           # Start selection
Enter           # Copy selection
Ctrl+b ]        # Paste

With vim keys in ~/.tmux.conf:

1
2
3
setw -g mode-keys vi
bind -T copy-mode-vi v send -X begin-selection
bind -T copy-mode-vi y send -X copy-selection-and-cancel

Status Bar Customization

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# Status bar colors
set -g status-style 'bg=#333333 fg=#ffffff'

# Left side: session name
set -g status-left '#[fg=green][#S] '
set -g status-left-length 20

# Right side: time and host
set -g status-right '#[fg=yellow]%H:%M #[fg=cyan]#H'

# Window status
setw -g window-status-current-style 'fg=black bg=yellow'
setw -g window-status-format ' #I:#W '
setw -g window-status-current-format ' #I:#W '

Practical Patterns

SSH + tmux Workflow

1
2
# Connect and attach in one command
ssh server -t 'tmux attach -t main || tmux new -s main'

Running Multiple Services

1
2
3
4
# Window per service
tmux new-window -n api 'cd api && npm start'
tmux new-window -n worker 'cd worker && python worker.py'
tmux new-window -n db 'docker-compose up postgres redis'

Log Monitoring Layout

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# Four-pane log view
tmux new-session -d -s logs
tmux split-window -h
tmux split-window -v
tmux select-pane -t 0
tmux split-window -v

tmux send-keys -t 0 'tail -f /var/log/nginx/access.log' Enter
tmux send-keys -t 1 'tail -f /var/log/nginx/error.log' Enter
tmux send-keys -t 2 'tail -f /var/log/app/app.log' Enter
tmux send-keys -t 3 'tail -f /var/log/app/error.log' Enter

tmux attach -t logs

Quick Reference

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Sessions
tmux new -s name           # Create named session
tmux ls                    # List sessions
tmux attach -t name        # Attach to session
tmux kill-session -t name  # Kill session

# From inside tmux
Ctrl+b d                   # Detach
Ctrl+b $                   # Rename session
Ctrl+b s                   # Session list

# Windows
Ctrl+b c                   # New window
Ctrl+b ,                   # Rename window
Ctrl+b n/p                 # Next/prev window
Ctrl+b w                   # Window list

# Panes
Ctrl+b %                   # Split vertical
Ctrl+b "                   # Split horizontal
Ctrl+b arrow               # Navigate
Ctrl+b z                   # Zoom toggle
Ctrl+b x                   # Kill pane

tmux is the safety net that lets you work on remote servers without fear. Learn it once, use it forever.