tmux
tmux keeps a shell running on the server after you disconnect. Start a long alignment or a pipeline inside a tmux session on the remote machine, detach, close the laptop, and attach again later to find the job still running and its output still on screen.
Sessions
Section titled “Sessions”# Start a named sessiontmux new -s rnaseq
# Attach to the session, or create it if it does not existtmux new -A -s rnaseq
# Start a command in a detached session and walk awaytmux new -d -s align 'bash run_alignment.sh > align.log 2>&1'
# List sessionstmux ls
# Attach to a named sessiontmux attach -t rnaseq
# End a session and everything running in ittmux kill-session -t rnaseqtmux ls prints one line per session with its name, its number of windows, and the time it was
created, and it marks the session you are attached to with (attached). Name every session; a
list of 0, 1, and 2 tells you nothing a week later.
A session started with new -d ends when its command exits, and the window goes with it. Send
the output to a log file, as above, so it survives.
The prefix key
Section titled “The prefix key”Every tmux binding starts with the prefix, Ctrl+b. Press it, release both keys, then press the
command key. The tables write the prefix as prefix.
| Key | Action |
|---|---|
prefix d |
Detach from the session; everything keeps running |
prefix $ |
Rename the session |
prefix s |
Pick a session from a list |
prefix ? |
List every key binding |
Windows
Section titled “Windows”A window is a full-screen tab inside a session, with its own shell.
| Key | Action |
|---|---|
prefix c |
Create a window |
prefix n |
Next window |
prefix p |
Previous window |
prefix 0 to prefix 9 |
Go to a window by number |
prefix , |
Rename the window |
prefix w |
Pick a window from a list |
prefix & |
Close the window, after a confirmation |
A pane splits one window into several shells side by side.
| Key | Action |
|---|---|
prefix % |
Split into left and right panes |
prefix " |
Split into top and bottom panes |
prefix and an arrow key |
Move to the pane in that direction |
prefix o |
Move to the next pane |
prefix q |
Show pane numbers; press a number to jump there |
prefix z |
Zoom the pane to the full window and back |
prefix Ctrl+arrow |
Resize the pane by one cell |
prefix Alt+arrow |
Resize the pane by five cells |
prefix Space |
Cycle through the preset layouts |
prefix ! |
Move the pane into a window of its own |
prefix x |
Close the pane, after a confirmation |
Copy mode and scrollback
Section titled “Copy mode and scrollback”tmux keeps its own scrollback for each pane. Copy mode lets you scroll it with the arrow keys
and Page Up or Page Down, search it, and copy from it. tmux uses the emacs keys unless
mode-keys is set to vi, or unless VISUAL or EDITOR contains vi.
| Action | vi keys | emacs keys |
|---|---|---|
| Enter copy mode | prefix [ |
prefix [ |
| Search up | ? |
Ctrl+r |
| Search down | / |
Ctrl+s |
| Start a selection | Space |
Ctrl+Space |
| Copy the selection | Enter |
Alt+w |
| Leave copy mode | q |
q |
| Paste | prefix ] |
prefix ] |
A starting configuration
Section titled “A starting configuration”Put these lines in ~/.tmux.conf, then reload the file with tmux source-file ~/.tmux.conf:
# Mouse support: click to select a pane, drag to resize, wheel to scroll.set -g mouse on
# vi keys in copy mode.setw -g mode-keys vi
# Lines of scrollback per pane. Read when a pane is created.set -g history-limit 100000
# Number windows and panes from 1, to match the keyboard.set -g base-index 1setw -g pane-base-index 1
# prefix r reloads this file.bind r source-file ~/.tmux.conf \; display-message "tmux.conf reloaded"mouse on alone makes the wheel scroll in some programs and not in others. The
tmux guide gives the wheel bindings that fix it and measures
what 100,000 lines of history cost in memory.
Recipes
Section titled “Recipes”Run a pipeline that outlives the connection
Section titled “Run a pipeline that outlives the connection”ssh labservertmux new -A -s rnaseqnextflow run nf-core/rnaseq -r 3.22.2 \ -profile test,podman --outdir test_resultsPress prefix d to detach and log out. Later, reconnect and attach:
ssh labservertmux attach -t rnaseqWatch a log next to the job
Section titled “Watch a log next to the job”- Start a session with
tmux new -s pipelineand launch the pipeline in it. - Press
prefix %to split the window into left and right panes. - In the right pane, run
tail -f pipeline.logorhtop. - Press
prefixand the left arrow to return to the pipeline. - Press
prefix zto give one pane the full window while you read it.
Reattach on a cluster with several login nodes
Section titled “Reattach on a cluster with several login nodes”A hostname such as hpc.example.edu often points to several login nodes, and a tmux session
lives on only one of them. Run hostname before you detach, and connect to that node by name to
attach again. Many clusters also forbid long computations on a login node, so keep tmux for the
shell that submits and watches jobs, and run the work through the scheduler.
Commands
Section titled “Commands”| Command | What it does |
|---|---|
tmux ls |
List sessions |
tmux new -s NAME |
Create a named session |
tmux new -A -s NAME |
Attach to a session, or create it |
tmux new -d -s NAME 'CMD' |
Run a command in a detached session |
tmux attach -t NAME |
Attach to a session |
tmux kill-session -t NAME |
End one session |
tmux kill-server |
End every session on the server |
tmux source-file ~/.tmux.conf |
Reload the configuration |
tmux show-options -g |
Show the global options in effect |
tmux list-keys |
List every key binding |
See also
Section titled “See also”- tmux: Sessions That Survive explains the alternate screen, scrolling, and a full configuration.
- SSH & Remote Servers covers keys, config files, and keepalives.
- SSH, scp & rsync is the quick reference for the connection itself.
- SLURM covers the scheduler that should run the heavy work.
Further reading
Section titled “Further reading”The tmux 3.6 manual defines session commands, copy mode, and configuration reloads. Its command examples show how a reload and a confirmation message share one binding.
The 3.6 binding tables define the default keyboard shortcuts. Compare the prefix, root, and copy-mode tables when a key behaves differently from the table on this page.