tmux · terminal · devops
tmux: A Pocket Companion
Never lose a remote session again. Persistent terminals, splits, and windows — the natural companion to SSH.
You SSH into a server, kick off a migration, and your wifi blinks — the connection drops and the job dies with it. tmux fixes this for good: it runs your terminal sessions on the server, so you can detach, walk away, lose your connection, and reattach later to find everything exactly where you left it. It’s the natural companion to our SSH pocket companion.
$ ssh deploy@web-01
$ tmux new -s deploy
# ...run a long migration... then Ctrl-b d to detach and disconnect safely
The mental model: sessions, windows, panes
Three nested ideas, and that’s the whole tool:
- A session is a workspace that lives on the server, independent of your connection. Detach from it, close your laptop — it keeps running.
- A window is a full-screen tab inside a session.
- A pane is a split within a window — two shells side by side.
Everything you do is one prefix key followed by a command. By default the prefix is
Ctrl-b. Press it, release, then press the command key.
Read Ctrl-b c as: prefix, then c
Throughout this guide, Ctrl-b means “press the prefix.” So Ctrl-b c is press
Ctrl-b, let go, press c. Many people remap the prefix to Ctrl-a (easier reach) —
we’ll do that in the config below.
Sessions: the part that saves you
$ tmux new -s work # start a named session
$ tmux ls # list sessions
$ tmux attach -t work # reattach to one
$ tmux kill-session -t work
Inside a session, Ctrl-b d detaches — your shell keeps running on the server. This
is the killer feature: start a deploy, detach, log off entirely, then tmux attach from
anywhere tomorrow and watch the tail end of the logs.
Run anything long-lived in tmux
Migrations, rsync of a big dataset, a dev server, a download — if it would hurt to
have it killed by a dropped SSH connection, start it inside tmux first. Ctrl-b d is
cheap insurance.
Windows: tabs inside the session
Ctrl-b c create a new window
Ctrl-b , rename the current window
Ctrl-b n / p next / previous window
Ctrl-b 0-9 jump to window by number
Ctrl-b w pick a window from a list
Ctrl-b & kill the current window
One window for the editor, one for logs, one for a shell — switch with Ctrl-b + a
number. The window list along the bottom status bar shows where you are.
Panes: splits within a window
Ctrl-b % split left/right (vertical divider)
Ctrl-b " split top/bottom (horizontal divider)
Ctrl-b o cycle to the next pane
Ctrl-b ←↑↓→ move to the pane in that direction
Ctrl-b z zoom the current pane to full-screen (toggle)
Ctrl-b x close the current pane
Ctrl-b { / } swap pane left / right
Edit code in one pane, tail logs in another, run commands in a third — all visible at
once. Ctrl-b z is the one to remember: zoom a pane to focus, press it again to come back.
Scrolling and copying
By default the mouse scrolls your terminal, not tmux’s history. Enter copy mode to scroll back and select text:
Ctrl-b [ enter copy mode (then scroll with arrows / PgUp / Ctrl-u)
Space start selection Enter copy it
Ctrl-b ] paste
q leave copy mode
/ and ? search forward / backward in the scrollback
A friendlier ~/.tmux.conf
A few lines make tmux far nicer. The big wins: a reachable prefix, mouse support, and sane split keys.
# easier prefix: Ctrl-a instead of Ctrl-b
unbind C-b
set -g prefix C-a
bind C-a send-prefix
set -g mouse on # scroll + click panes/windows with the mouse
set -g base-index 1 # windows start at 1, not 0
setw -g pane-base-index 1
# split with | and - (more intuitive than % and ")
bind | split-window -h
bind - split-window -v
# reload config without restarting
bind r source-file ~/.tmux.conf \; display "reloaded"
Reload after editing with Ctrl-b : then source-file ~/.tmux.conf, or just Ctrl-a r
once the binding above is in place.
When you’re stuck
- Keys doing nothing? You probably forgot the prefix. Press
Ctrl-b(orCtrl-a) first, then the command. - Lost a session?
tmux lsfrom the server lists everything still running;tmux aattaches to the most recent. - Stuck in copy mode? Press
q. - Want a command by name?
Ctrl-b :opens a command prompt (e.g.:kill-server).
Pocket cheat-sheet
| Do | Keys |
|---|---|
| New / list / attach session | tmux new -s x / tmux ls / tmux a -t x |
| Detach (keep running) | Ctrl-b d |
| New / rename / switch window | Ctrl-b c / Ctrl-b , / Ctrl-b 0-9 |
| Split vertical / horizontal | Ctrl-b % / Ctrl-b " |
| Move between panes | Ctrl-b ←↑↓→ |
| Zoom a pane (toggle) | Ctrl-b z |
| Copy mode / search | Ctrl-b [ / / |
| Command prompt | Ctrl-b : |
Pair tmux with SSH and a dropped connection stops being a disaster — it’s just a tmux a
away from picking up exactly where you left off. man tmux is the full reference when you
outgrow this card.
Get new field notes by email
Occasional, practical write-ups on building and shipping software. No spam — unsubscribe anytime.