Skip to content

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.

Terminal window
# Start a named session
tmux new -s rnaseq
# Attach to the session, or create it if it does not exist
tmux new -A -s rnaseq
# Start a command in a detached session and walk away
tmux new -d -s align 'bash run_alignment.sh > align.log 2>&1'
# List sessions
tmux ls
# Attach to a named session
tmux attach -t rnaseq
# End a session and everything running in it
tmux kill-session -t rnaseq

tmux 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.

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

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

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 ]

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 1
setw -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.

Run a pipeline that outlives the connection

Section titled “Run a pipeline that outlives the connection”
Terminal window
ssh labserver
tmux new -A -s rnaseq
nextflow run nf-core/rnaseq -r 3.22.2 \
-profile test,podman --outdir test_results

Press prefix d to detach and log out. Later, reconnect and attach:

Terminal window
ssh labserver
tmux attach -t rnaseq
  1. Start a session with tmux new -s pipeline and launch the pipeline in it.
  2. Press prefix % to split the window into left and right panes.
  3. In the right pane, run tail -f pipeline.log or htop.
  4. Press prefix and the left arrow to return to the pipeline.
  5. Press prefix z to 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.

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

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.