tmux cheat sheet
Keep sessions alive when SSH drops, split the terminal into panes, and work in several windows on one server.
Why tmux?
When your SSH connection drops, everything running in it dies: an upgrade, a big copy, a database import. tmux runs your shell on the server inside a session that keeps running when you disconnect. You reconnect and continue where you were. On top you get several windows and split panes in one SSH connection.
tmux is installed on Ubuntu Server by default.
The prefix key
Almost every tmux shortcut starts with the prefix: press Ctrl+B, release, then press the next key. Written below as Prefix + key.
Sessions
| What | Command / keys |
|---|---|
| New session with a name | tmux new -s work |
| Detach (session keeps running) | Prefix d |
| List sessions | tmux ls |
| Re-attach | tmux attach -t work (or just tmux a) |
| Attach or create if missing | tmux new -A -s work |
| Switch between sessions | Prefix s |
| Rename the session | Prefix $ |
| End a session | exit in its last shell, or tmux kill-session -t work |
Typical use: ssh srv01, tmux new -A -s work, start the long job, Prefix d, log out. Later: ssh srv01, tmux a.
Windows (like tabs)
| What | Keys |
|---|---|
| New window | Prefix c |
| Next / previous window | Prefix n / Prefix p |
| Go to window 0-9 | Prefix 0…9 |
| List and pick | Prefix w |
| Rename window | Prefix , |
| Close window | exit or Prefix & |
Panes (split screen)
| What | Keys |
|---|---|
| Split left/right | Prefix % |
| Split top/bottom | Prefix " |
| Move between panes | Prefix + arrow keys |
| Zoom a pane (toggle full size) | Prefix z |
| Resize | Prefix + Ctrl+arrow keys |
| Close pane | exit or Prefix x |
| Same input to all panes (e.g. several SSH sessions) | Prefix : then setw synchronize-panes |
Scrolling and copying
| What | Keys |
|---|---|
| Scroll mode | Prefix [, then arrow keys / PgUp, q to leave |
| Search in scroll mode | ? (up) or / (down) |
| Use the mouse to scroll and select | add set -g mouse on to ~/.tmux.conf |
With mouse mode on, hold Shift (or Option on macOS) to select text for your computer’s clipboard.
~/.tmux.conf
set -g mouse on # scroll and click with the mouse
set -g history-limit 50000 # longer scrollback
set -g base-index 1 # windows start at 1, not 0
bind | split-window -h # Prefix | to split left/right
bind - split-window -v # Prefix - to split top/bottom
Reload: tmux source-file ~/.tmux.conf.
Note:
screenis the older tool that does the same. If a colleague left ascreensession running:screen -ls,screen -r NAME, detach withCtrl+Ad.