tmux
Overview
tmux is a terminal multiplexer: multiple windows and panes inside detachable sessions that survive SSH disconnects. Prefer it over nohup for interactive long-lived remote work, and over ad-hoc background jobs when you need to reattach and see output. For production services still prefer systemd.
Syntax
tmux [command] [options]
tmux [-L socket-name] [-S socket-path] [-f file]Most daily use is either bare tmux or a short subcommand (new, attach, ls, kill-session).
Install (Ubuntu)
sudo apt update
sudo apt install tmux
tmux -VPrefix key
Default prefix: Ctrl-b, then a command key. (Many people rebind to Ctrl-a.)
| After prefix | Action |
|---|---|
c |
New window |
, |
Rename window |
n / p |
Next / previous window |
0–9 |
Select window by index |
w |
Window list |
& |
Kill window |
% |
Split pane left/right |
" |
Split pane top/bottom |
o / arrow keys |
Cycle / move between panes |
; |
Last pane |
x |
Kill pane (confirm) |
z |
Zoom toggle (pane fullscreen) |
{ / } |
Swap panes |
Space |
Cycle pane layouts |
d |
Detach session |
s |
Session list |
$ |
Rename session |
[ |
Copy / scroll mode |
] |
Paste buffer |
? |
List keys |
: |
Command prompt |
Common CLI commands
| Command | Description |
|---|---|
tmux |
New session (default name) |
tmux new -s NAME |
Named session |
tmux new -d -s NAME [cmd] |
Detached session (optional command) |
tmux ls |
List sessions |
tmux attach -t NAME |
Attach |
tmux attach -d -t NAME |
Attach and detach others |
tmux switch -t NAME |
Switch (from inside tmux) |
tmux kill-session -t NAME |
Kill session |
tmux kill-server |
Kill all sessions (careful) |
tmux list-windows -t NAME |
Windows in session |
Key Use Cases
- Survive laptop sleep / SSH drops during deploys
- Pair multiple panes (logs + editor + shell)
- Long builds or trainers you can reattach to
- Named sessions per project (
tmux new -s api)
Examples with Explanations
Example: create, detach, reattach
tmux new -s deploy
# ... work ...
# Ctrl-b d
tmux ls
tmux attach -t deploy
# short form:
tmux a -t deployCore workflow. Detach leaves processes running on the server.
Example: start detached job session
tmux new -d -s train 'python3 train.py 2>&1 | tee train.log'
tmux attach -t trainSession exists immediately; attach when you want the TTY.
Example: nested SSH-safe habit
ssh host
tmux new -s work || tmux attach -t workCreate-or-attach pattern for a daily session.
Example: splits for ops
Inside a session:
Ctrl-b %— vertical split (side by side)Ctrl-b "— horizontal splitCtrl-b z— zoom the pane you care about- Run
journalctl -fu nginxin one pane, shell in another
Example: scripted window layout
tmux new -d -s app
tmux rename-window -t app:0 shell
tmux new-window -t app -n logs 'journalctl -f'
tmux new-window -t app -n top 'htop'
tmux split-window -h -t app:shell
tmux attach -t appGood for standardizing a “ops dashboard” session.
Example: send keys to a session (automation)
tmux new -d -s patch
tmux send-keys -t patch 'sudo apt update && sudo apt upgrade' C-mUse sparingly; prefer scripts over pseudo-interactive automation when possible.
Example: minimal ~/.tmux.conf
set -g mouse on
set -g history-limit 50000
set -g base-index 1
setw -g pane-base-index 1
set -g renumber-windows on
# rebind prefix to Ctrl-a (optional):
# unbind C-b
# set -g prefix C-a
# bind C-a send-prefix
Reload from inside tmux: Ctrl-b : then source-file ~/.tmux.conf.
Example: copy mode (scrollback)
Ctrl-b [ → move with arrows/PageUp → Space begin selection → Enter copy (bindings vary with mode-keys vi/emacs) → Ctrl-b ] paste.
With mouse on, scroll and select often work directly.
Safety
tmux kill-serverends all sessions for that server socket — confirmtmux lsfirst.- Detached sessions still consume resources; clean up dead project sessions.
- Do not run secrets printing in shared sessions.
- Nested tmux (local + remote) confuses prefixes — use a different remote prefix or SSH directly into remote tmux only.
Notes & Pitfalls
- Environment variables are captured at session start; re-login SSH agent sockets often need
tmux setenvor plugin tooling after laptop sleep. TERM/COLORTERMmismatches cause weird colors — prefertmux -2legacy force or correctterminal-overridesin conf for modern terminals.- Clipboard integration needs
xclip/wl-clipboardand conf bindings on desktop Linux. - Version differences: Ubuntu LTS tmux may lag plugin ecosystem features — check
tmux -V. - Prefer one session per task rather than infinite windows without names.
Additional Resources
man tmuxtmux list-keys/tmux list-commands