A server behind your terminal
When you start tmux, it creates a server process in the background. The server holds the pseudo-terminals and the shells, and the client is the program that connects your terminal to the server. Closing your terminal window closes only the client. The server and its shells keep running, which is why a long job started in tmux survives a broken connection.
The server listens on a socket file. By default the socket lives in a directory under /tmp that belongs to your user. The -L option names a different socket, which lets you run a separate server for an experiment without touching your main one. The lab uses that option so the exercises never clash with each other.
Sessions, windows, and panes
A session is a named group of windows. A window is a full-screen view inside a session, and it can have one or more panes. A pane is a single pseudo-terminal, with its own shell. The hierarchy is session, then window, then pane, and each level has its own list that you can print with a list command.
tmux -L demo new-session -d -s build -n shellThe command above creates a session named build with a first window named shell. The -d option starts it detached, so the command returns without attaching. That is how scripts create sessions, and a person can attach later with attach-session.
The prefix key
Most tmux commands are typed after a prefix key. The default prefix is Ctrl-b. Press the prefix, release it, and then press a key. Prefix followed by c creates a window, prefix followed by a number selects a window, prefix followed by a percent sign splits the pane left and right, and prefix followed by a double quote splits it top and bottom. Prefix followed by d detaches the client, and the session keeps running.
The prefix is a configuration choice. Many people change it to Ctrl-a, because that key is easier to reach, and they unbind the old one so that the two do not conflict. The lab exercise does exactly that in a config file.
Configuration in a file
tmux reads its settings from a file, usually named tmux.conf. A setting uses the set command, and the -g option makes it global. The bind command attaches a key to a tmux command, and unbind removes a binding. Two common changes are the base index, which numbers windows from one instead of zero, and the prefix.
set -g prefix C-a
unbind C-b
bind C-a send-prefix
set -g base-index 1The line with send-prefix is important. Without it, pressing Ctrl-a twice does nothing useful, because the prefix key is consumed. With it, a second press sends a real Ctrl-a to the program in the pane, which matters for programs that use that key, such as the shell line editor in some setups.
Listing and checking state
The list commands print what the server holds. list-sessions shows the sessions, list-windows shows the windows of one session, and list-panes shows the panes of one window. The show-options command prints the value of an option, so you can confirm that a config file took effect. These commands work from a script, which makes them good for tests.
tmux -L demo show-options -g prefixA tmux command with no server running starts one. A command that asks for a session that does not exist fails with a message that names the session. Read that message first; it is usually a typo or a server on a different socket.
Stopping cleanly
kill-server stops the server and every session it holds. Use it to clean up after an experiment on a private socket, and do not run it on your main socket unless you mean to stop everything. Killing a single session with kill-session is safer when you only want to remove one.
The lab machine has tmux installed but no man pages. Run tmux with the -V option to print the version, and use the built-in list of commands in the tmux help key on a real terminal, or read the wiki on the project site.
Targets and sending keys
Commands that act on a window or a pane take a target. A target is written as the session name, a colon, the window name or index, and optionally a period and the pane index. A target such as build:logs names the logs window of the build session. The send-keys command types text into a target, so a script can start a command in another pane without attaching. The text is sent as if it had been typed, and a final key name such as Enter runs it.
Scripts that use send-keys should be careful about quoting, because the text is interpreted by tmux before it reaches the shell in the pane. Keep the text simple, and test it on a private socket before you rely on it in a real setup.
Attaching from another terminal
Attaching a client needs a real terminal, so it cannot run in a script that has no terminal attached. A person connects with attach-session and the session name, and the same server then shows the same windows in every client. Two clients attached to one session see the same panes, which is useful for pair work and for watching a long job from a second place. Detaching with the prefix followed by d leaves the session running for the next client.
Zooming and resizing panes
The zoom command toggles one pane to fill the whole window and back, which is the quickest way to read a long log in a split layout. Resizing with the arrow keys after the prefix moves the border between panes, and the layout commands arrange all the panes in a standard pattern, such as even horizontal or tiled. Layouts are a view on the same panes, so none of them stops a process.