SSH into a server, start a long-running process, lose connection, lose your work. tmux solves this. It creates persistent terminal sessions that survive disconnects and lets you manage multiple terminals from one window.

Basic Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Start new session
tmux

# Start named session
tmux new -s mysession

# List sessions
tmux ls

# Attach to session
tmux attach -t mysession
tmux a -t mysession  # short form

# Attach to last session
tmux attach
tmux a

# Detach from session (inside tmux)
Ctrl-b d

# Kill session
tmux kill-session -t mysession

The Prefix Key

All tmux commands start with a prefix key (default: Ctrl-b), then another key:

CCCCCCCtttttttrrrrrrrlllllll-------bbbbbbbdcnp"%?#######DNNPSSSeeerpphtwxelloatviiwcwitthiwoaniuhvldnsoelodrrwowitbwiziinocndnadotliwanlgs

Windows (Tabs)

CCCCCCCtttttttrrrrrrrlllllll-------bbbbbbbc,np0w&-9#######CRNPGLKreeroiienxeslaatvttltmioeewowciuwiuncnsinreudndrwrowdoerwiownwenwstindnton(wdwuiiowmnnwibtdneeodrrwoawctive)

Panes (Splits)

CCCCCCCCCCttttttttttrrrrrrrrrrllllllllll----------bbbbbbbbbb"%;qqzx{}0-9##########SSCTSGTKMMppyohooioollcgoglvviilgwtgleettelolepecpphvtapuaaoehlnaprnnrrraenareeitoseneziutnenlrocgunteinahamuzfgtlcbmopthalpteboatlyairemnlnvsreyee(sfpualnlescreen)

Resize Panes

CCCC#CtttttrrrrOrllllrl-----bbbbhbo::::lCrrrrdteeeerssssCliiiit-zzzzreeeel/-----ppppb/aaaannnna/eeeend----DULRpr1111e0000ssa####rrDULRoopeiwwfgn1thk0te11y0l01si0lncieocnsloeulsmunmsns

Copy Mode

tmux has a built-in copy/paste system:

Cq#SE?nNCtpntrIatrlncel-er-bcbo[p]ymod##e#######EESCSSNPPnxvtoeeeratiiapaaxesetryrrtvtrbtcciecishhsoconseeubopdelfbasupyileoarfynecrccsfmgctwkheemostiawarod)iorarrde:ondrecendshulrtesult

Scrollback

CPgGqtargle-Ubp/[Down#####ESGGEncooxtrieotttrloolctbooopptytommodetoscroll

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
34
35
36
# Change prefix to Ctrl-a (like screen)
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

# Enable mouse
set -g mouse on

# More scrollback
set -g history-limit 50000

# Faster escape
set -sg escape-time 0

# Better splits (current path)
bind '"' split-window -c "#{pane_current_path}"
bind % split-window -h -c "#{pane_current_path}"

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

# Vi mode
setw -g mode-keys vi

# Better copy mode
bind -T copy-mode-vi v send -X begin-selection
bind -T copy-mode-vi y send -X copy-selection-and-cancel

# Status bar
set -g status-style 'bg=#333333 fg=#ffffff'
set -g status-left '[#S] '
set -g status-right '%H:%M %d-%b'

Reload config without restart:

Ctrl-b:source-file~/.tmux.conf

Session Management

From Outside tmux

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# New session with window name
tmux new -s work -n editor

# Attach or create
tmux new -A -s work

# Send command to session
tmux send-keys -t mysession 'npm start' Enter

# Run command in new window
tmux new-window -t mysession 'htop'

From Inside tmux

CCCCttttrrrrllll----bbbbs$()####LRPNieresnextavtmiseoseuesssssesissioseonisnsosni(oinnteractive)

Practical Workflows

Development Setup

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#!/bin/bash
# dev-session.sh

SESSION="dev"

tmux new-session -d -s $SESSION -n "editor"
tmux send-keys -t $SESSION:editor "cd ~/project && vim" Enter

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

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

tmux select-window -t $SESSION:editor
tmux attach -t $SESSION

Split Layout Script

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#!/bin/bash
# monitoring.sh

tmux new-session -d -s monitoring

# Main pane - htop
tmux send-keys 'htop' Enter

# Right pane - logs
tmux split-window -h
tmux send-keys 'tail -f /var/log/syslog' Enter

# Bottom right - disk
tmux split-window -v
tmux send-keys 'watch df -h' Enter

# Select first pane
tmux select-pane -t 0

tmux attach -t monitoring

Remote Pair Programming

1
2
3
4
5
6
7
# Host creates session
tmux new -s pair

# Guest attaches (same server)
tmux attach -t pair

# Both see the same session in real-time

Useful Commands

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# List all key bindings
tmux list-keys

# Show all options
tmux show-options -g

# Capture pane contents
tmux capture-pane -p > output.txt

# Clear history
tmux clear-history

# Swap panes
Ctrl-b :swap-pane -U  # With pane above
Ctrl-b :swap-pane -D  # With pane below

# Move window
Ctrl-b :move-window -t 3

# Join pane from another window
Ctrl-b :join-pane -s :2  # From window 2

# Break pane into new window
Ctrl-b !

tmux + SSH

Automatic Attach

Add to .bashrc on remote server:

1
2
3
if [[ -z "$TMUX" ]] && [[ -n "$SSH_CONNECTION" ]]; then
    tmux attach -t main || tmux new -s main
fi

Persistent Remote Session

1
2
# SSH and attach to tmux
ssh server -t 'tmux attach || tmux new'

Plugins (TPM)

Install tmux plugin manager:

1
git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm

Add to ~/.tmux.conf:

1
2
3
4
5
6
7
8
# List of plugins
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'
set -g @plugin 'tmux-plugins/tmux-resurrect'  # Save/restore sessions
set -g @plugin 'tmux-plugins/tmux-continuum'  # Auto-save

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

Install plugins: Ctrl-b I

Quick Reference

ActionKeys
DetachCtrl-b d
New windowCtrl-b c
Next windowCtrl-b n
Split horizontalCtrl-b "
Split verticalCtrl-b %
Switch paneCtrl-b o
Zoom paneCtrl-b z
Copy modeCtrl-b [
PasteCtrl-b ]
List sessionsCtrl-b s
Rename windowCtrl-b ,

tmux transforms how you work with remote servers. No more lost sessions, no more juggling terminal windows. Start with tmux, learn the prefix pattern, and gradually customize. Once you’re used to it, you’ll wonder how you worked without it.