T.05 · Terminal Fundamentals

tmux

Many terminals in one window. Sessions that survive SSH disconnections. Pane buffers you can save as text files.

tmux is a terminal multiplexer. It lets you run multiple terminal sessions inside a single window, detach from them, and reattach later — even from a different machine. The win that hooks most people: if your SSH connection drops mid-lab, your work doesn't die. The shell keeps running, the file copy keeps going, the long-running test keeps progressing. When you reconnect, you reattach exactly where you left off.

For this course there is one bonus: tmux can capture the entire scrollback of any pane to a text file. That's a separate path to "save my work for the deliverable" alongside script and asciinema.

Install

$ sudo apt install tmux # Debian / Ubuntu $ sudo dnf install tmux # Fedora / RHEL / Rocky $ brew install tmux # macOS

The three concepts

tmux organizes terminals into a three-level hierarchy:

SESSION — a named workspace; survives SSH disconnects │ ├── WINDOW 0 (like a tab in a browser) │ ├── PANE 0 (a single shell) │ └── PANE 1 │ ├── WINDOW 1 │ └── PANE 0 │ └── WINDOW 2 └── PANE 0

The prefix key

Every tmux command starts with a prefix key, then a second key. The default prefix is Ctrl+b. Throughout the rest of this page, you'll see notation like Ctrl+b d — that means: press Ctrl+b, release it, then press d.

Many people remap the prefix to Ctrl+a (matching GNU screen). To do that, add set -g prefix C-a + unbind C-b + bind C-a send-prefix to your ~/.tmux.conf. For this page we'll assume the default Ctrl+b.

The first five minutes

Start a session

$ tmux # start a new unnamed session $ tmux new -s lab3 # start a new named session called "lab3"

Detach — the killer feature

Ctrl+b d

tmux exits and drops you back at your normal shell. But the session keeps running. Close the terminal window, disconnect your SSH, walk away — the session is still there.

Reattach

$ tmux attach # attach to the most recent session $ tmux attach -t lab3 # attach to a specific named session $ tmux ls # list all sessions running on this machine

Common workflow

SSH into a lab box, start tmux, run a long task (a network scan, a backup, a fuzzing run), detach, close your laptop. Re-open the laptop the next morning, SSH back in, reattach the tmux session, the task has finished, the output is right there. Your work is not tied to your SSH connection's lifetime.

Cheat sheet — the keys you'll use daily

KeystrokeWhat it does
Sessions
Ctrl+b dDetach (session keeps running)
Ctrl+b $Rename the current session
Ctrl+b sPick a session from the list (when you have several)
Windows
Ctrl+b cCreate a new window
Ctrl+b nNext window
Ctrl+b pPrevious window
Ctrl+b 0..9Jump to window number 0..9
Ctrl+b ,Rename the current window
Ctrl+b wInteractive window picker
Panes (splits)
Ctrl+b %Split vertically (panes side-by-side)
Ctrl+b "Split horizontally (panes stacked)
Ctrl+b arrowMove between panes
Ctrl+b zToggle "zoom" on current pane (full-screen / restore)
Ctrl+b xKill the current pane (asks first)
Ctrl+b { / }Swap pane with the previous / next pane
Scrolling & copying
Ctrl+b [Enter copy mode (now you can scroll back with arrows / Page Up)
q (in copy mode)Exit copy mode back to normal
Ctrl+b ]Paste the most recent copy buffer
Help
Ctrl+b ?Show every key binding (with /search support)

Saving a pane to a file — for lab submission

tmux can dump the contents of a pane (including scrollback) to a text file. This is the alternative to script when you've already been working in tmux:

# In the current pane, save the visible buffer + entire scrollback to a file Ctrl+b : capture-pane -S - Ctrl+b : save-buffer ~/lab3-pane.txt # Or from the regular shell, target a specific session/window/pane: $ tmux capture-pane -t lab3:0.0 -S - -p > ~/lab3.txt

-S - means "from the start of the scrollback." Without it, you only get the currently visible portion. -p prints to stdout (useful for piping to a file from outside tmux). For end-of-class deliverables this gives you a clean text file with everything that scrolled past during the session.

A ~/.tmux.conf worth having

Drop this in ~/.tmux.conf for a more humane experience:

# Use Ctrl+a as prefix instead of Ctrl+b (matches screen) set -g prefix C-a unbind C-b bind C-a send-prefix # Sane splits: | and - bind | split-window -h bind - split-window -v # Mouse support: click panes, scroll, drag borders set -g mouse on # Bigger scrollback set -g history-limit 10000 # Status bar: cleaner, with date and hostname set -g status-right '#H | %Y-%m-%d %H:%M'

Reload without restarting:

Ctrl+b : source-file ~/.tmux.conf
The point

tmux gives you three things: persistent sessions that survive SSH drops, splits so you can have an editor and a shell side by side, and pane capture so your scrollback isn't ephemeral. The learning curve is real but small — ten keystrokes will get you 90% of the value.

The day you start a long-running task on a remote box, detach with Ctrl+b d, close your laptop, walk away, come back, reattach, and find the task done — that's when tmux becomes permanent muscle memory.

References

Formatted in APA 7. Alphabetized by first author's last name.

  1. Hogan, B. P. (2016). tmux 2: Productive mouse-free development. Pragmatic Bookshelf.
  2. Marriott, N. (n.d.). tmux: A terminal multiplexer. https://github.com/tmux/tmux/wiki
  3. tmux contributors. (n.d.). tmux(1): Linux manual page. https://man7.org/linux/man-pages/man1/tmux.1.html