tmux on one page
Everything the course teaches. Keys are written Ctrl-b then a key: press Ctrl+b, let go, then press the key. If you changed the prefix (lab 10), use Ctrl-a instead.
The model
server → sessions → windows → panes. The tmux server runs in the background and owns everything. Your terminal is a client: detaching disconnects the client, and the server and its programs carry on. That's why jobs in tmux survive closed tabs and dropped connections.
Sessions
| Do | Command | Key |
|---|---|---|
| New session | tmux new -s name |
|
| New, in the background | tmux new -s name -d |
|
| List | tmux ls |
Ctrl-b s (pick from a list) |
| Attach | tmux attach -t name |
|
| Detach | Ctrl-b d |
|
| Next / previous session | Ctrl-b ) / Ctrl-b ( |
|
| Rename | tmux rename-session -t old new |
Ctrl-b $ |
| Close | tmux kill-session -t name |
|
| Exists? (for scripts) | tmux has-session -t name |
Windows
| Do | Command | Key |
|---|---|---|
| New window | tmux new-window -n name |
Ctrl-b c |
| Rename | tmux rename-window name |
Ctrl-b , |
| Next / previous | Ctrl-b n / Ctrl-b p |
|
| Go to window 2 | Ctrl-b 2 |
|
| Pick from a list | Ctrl-b w |
|
| Close | exit in its last pane |
Ctrl-b & |
Panes
| Do | Command | Key |
|---|---|---|
| Split side by side | tmux split-window -h |
Ctrl-b % |
| Split top and bottom | tmux split-window -v |
Ctrl-b " |
| Move between panes | Ctrl-b + arrow, Ctrl-b o |
|
| Zoom in / out | Ctrl-b z |
|
| Rearrange | tmux select-layout even-horizontal |
Ctrl-b Space (cycle) |
| Close | exit |
Ctrl-b x |
Copy mode (vi keys: set -g mode-keys vi)
| Do | Key |
|---|---|
| Enter copy mode | Ctrl-b [ |
| Search up / down | ?text / /text, then n for the next match |
| Start selection, copy | Space, then Enter |
| Paste | Ctrl-b ] |
| Leave | q |
Without attaching: tmux capture-pane -p -S - -t name prints a pane's whole history.
Config: ~/.tmux.conf
set -g prefix C-a # Ctrl-a as the prefix
unbind C-b # free the old one
bind C-a send-prefix # Ctrl-a Ctrl-a sends a real Ctrl-a
set -g mouse on # click panes, drag borders, scroll
set -g base-index 1 # optional: windows from 1 (default 0)
set -g history-limit 10000 # more scrollback per pane
set -g mode-keys vi # vi keys in copy mode
bind | split-window -h # Ctrl-a | splits side by side
The config is only read when the server starts. After editing: tmux source-file ~/.tmux.conf. Errors show as file:line: message, one at a time.
Scripting
Every key runs a tmux command, and tmux <command> from a shell does the same. Targets: session:window.pane, e.g. build:0.1.
#!/bin/bash
# A workspace that's safe to run twice
if ! tmux has-session -t dev 2>/dev/null; then
tmux new-session -d -s dev -n editor
tmux new-window -t dev -n server
tmux split-window -v -t dev:server
tmux send-keys -t dev:server.1 'tail -f logs/app.log' Enter
tmux select-window -t dev:editor
fi
tmux attach -t dev