tmux Basics: Sessions, Windows and Panes on Linux
Learn how tmux sessions, windows and panes fit together, and how to detach and reattach so long-running terminal work keeps going after you leave.
Contents
tmux is a terminal multiplexer. It lets you run several programs in one terminal, detach from them while they keep running, and reattach later, even from a different terminal. This guide covers the three building blocks (sessions, windows and panes) and the commands and keys you need day to day.
How the pieces fit together
According to the tmux wiki, a session groups one or more windows. Each session has a unique name, and unnamed sessions are numbered from 0. A window groups one or more panes and has a name and an index. A pane holds one terminal and the program running in it, and belongs to exactly one window.
In short: a session contains windows, and a window is split into panes.
Install tmux
The tmux wiki lists apt install tmux, dnf install tmux and pacman -S tmux. The non-interactive forms:
apt-get update && apt-get install -y tmuxdnf install -y tmuxpacman -Sy --noconfirm tmuxStart, detach and reattach
Create a named session with new-session (alias new). The -s option sets the name:
tmux new -s workInside the session, tmux commands are triggered with a prefix key. The default prefix is Ctrl+b: press it, release it, then press the next key. To detach, press Ctrl+b then d. The programs in the session keep running.
Back in a normal shell, list sessions with tmux ls and reattach with attach-session (alias attach). Without -t, it attaches to the most recently used unattached session; with -t you pick one:
tmux ls
tmux attach -t workWindows and panes: the key bindings
Press the prefix (Ctrl+b) first, then:
| Keys | Action |
|---|---|
c | Create a new window |
% | Split the pane left/right |
" | Split the pane top/bottom |
| Arrow key | Move to the pane in that direction |
o | Go to the next pane |
q | Show pane numbers; press a number to select it |
w | Tree view of sessions with their windows |
s | Tree view of sessions only |
d | Detach |
Script a layout without attaching
Every key binding has a matching command, and the -d option of new-session creates a session without attaching to it. That makes it easy to build a layout from a script and to check it. The following builds a session named demo with two windows, splits one into two panes, then lists the result:
tmux new-session -d -s demo -n editor
tmux new-window -t demo -n logs
tmux split-window -h -t demo:logs
tmux list-windows -t demo
tmux list-panes -t demo:logsHere new-window -n names the window, and split-window -h splits horizontally (the manual notes that vertical is the default). The -t option picks the target session, window or pane.
You can also send keystrokes to a pane and read its contents back with send-keys and capture-pane -p, which prints to standard output instead of a buffer:
tmux send-keys -t demo:logs.0 'echo hello from tmux' Enter
sleep 1
tmux capture-pane -p -t demo:logs.0 | grep 'hello from tmux'has-session exits with 0 if a session exists and 1 if it does not, which is useful in scripts. Combined with new-session -A, which attaches if the session already exists, you can use one command for both cases:
tmux new-session -A -s workRename and clean up
Rename a session with rename-session, and remove one with kill-session, which closes windows linked only to that session and detaches its clients:
tmux rename-session -t demo staging
tmux has-session -t staging && echo "staging exists"
tmux kill-session -t staging
tmux has-session -t staging || echo "staging is gone"kill-server ends the server, all clients and all sessions. The wiki notes that kill-session has no default key binding; run it from the command prompt (Ctrl+b then :).
Related reading
Long jobs in a tmux pane can fill a disk. To keep an eye on that, see check disk usage with df, du and ncdu. For network checks from a second pane, see the ss command.
Sources
This article was drafted with AI assistance, fact-checked against the sources listed above and reviewed by a human editor before publication. Spotted an error? See our corrections policy.