How to Set Up and Use tmux for Terminal Multiplexing
Running multiple shell sessions inside one terminal window, splitting panes, and, most importantly, keeping sessions alive across disconnects.
tmux (terminal multiplexer) lets you run multiple shell sessions inside a single terminal window, organized into windows and panes, and — critically — keeps those sessions running even if your terminal disconnects entirely.
Step 1: install tmux
# Debian/Ubuntu
sudo apt install tmux
# macOS (Homebrew)
brew install tmux
# FreeBSD
pkg install tmux
Step 2: start a new named session
tmux new -s work
Naming your session (work, in this example) makes it easy to identify and reattach to later, rather than relying on tmux’s default numeric session IDs.
Step 3: understand the prefix key
Ctrl-b (tmux's default prefix)
Almost every tmux command is triggered by pressing the prefix key first, then a follow-up key — covered in more depth in this blog’s dedicated tmux keybinding troubleshooting post if you run into conflicts with other applications.
Step 4: split your window into panes
Ctrl-b then % (split vertically)
Ctrl-b then " (split horizontally)
Ctrl-b then arrow key (move between panes)
Panes let you view and interact with multiple shells simultaneously within one window, without needing multiple separate terminal windows.
Step 5: create additional windows within the same session
Ctrl-b then c (create a new window)
Ctrl-b then n (next window)
Ctrl-b then p (previous window)
Ctrl-b then 0-9 (jump directly to window number)
Windows function like tabs — each holds its own layout of one or more panes, useful for keeping logically separate tasks visually separated within the same tmux session.
Step 6: detach from a session without ending it
Ctrl-b then d
Detaching leaves the entire session — every running process inside it — alive in the background, while returning your terminal to its normal state outside tmux.
Step 7: reattach to a running session, including after a disconnect
tmux attach -t work
This is tmux’s single most valuable capability: a session started over SSH survives a dropped connection, a closed laptop lid, or a deliberate detach entirely — reattaching from anywhere picks up exactly where you left off, including any long-running process still executing inside it.
Step 8: list all currently running sessions
tmux ls
Useful when you have multiple named sessions running and need to confirm which ones are still active before reattaching to a specific one.
Step 9: customize tmux via its configuration file
~/.tmux.conf
Common customizations include remapping the prefix key, enabling mouse support (set -g mouse on), and adjusting the status bar’s appearance — changes take effect after tmux source-file ~/.tmux.conf or restarting tmux entirely.
Why tmux’s session persistence is the actual killer feature
Running a long build, a long-lived monitoring command, or simply wanting your exact terminal layout to survive a network hiccup are all solved by the same underlying tmux capability — a session existing independently of any specific terminal connection to it, which is a fundamentally different guarantee than a normal shell session provides on its own.
Where tmux itself came from
Nicholas Marriott wrote the first version of tmux in 2007, initially as an OpenBSD-specific prototype, explicitly motivated by dissatisfaction with GNU Screen’s older, harder-to-extend codebase — Marriott specifically wanted a design clean enough to actually build on, including features (like sharing one window across multiple attached clients simultaneously) that felt impractical to add to Screen’s existing internals. tmux joined the OpenBSD base system in 2009, and the portable version most Linux and macOS installations run today descends directly from that same 2007 origin — meaning the client-server architecture and prefix-key model this guide walks through has been stable, actively developed core design for close to two decades now.
Step 10: copy text with the keyboard using vi-style copy mode
# in ~/.tmux.conf
setw -g mode-keys vi
Ctrl-b then [ (enter copy mode)
v (begin selection, once mode-keys is vi)
y (copy selection, exit copy mode)
Ctrl-b then ] (paste)
Copy mode lets you scroll back through a pane’s history and select text entirely from the keyboard, without touching the mouse or relying on your terminal emulator’s own selection — genuinely useful for grabbing output that’s scrolled past the visible screen. Setting mode-keys to vi swaps tmux’s default (Emacs-style) copy-mode bindings for vi-style navigation and selection, which most users who are already comfortable with vi’s motions find considerably more natural than memorizing a separate, tmux-specific keybinding set.
Step 11: manage plugins with the Tmux Plugin Manager
# in ~/.tmux.conf, near the end
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-resurrect'
run '~/.tmux/plugins/tpm/tpm'
TPM (Tmux Plugin Manager) handles installing, updating, and removing tmux plugins declared in your config, rather than manually cloning plugin repositories yourself — tmux-resurrect, for saving and restoring an entire session layout across a reboot, and tmux-yank, for integrating copy-mode selections with the system clipboard, are two of the most commonly installed. The run line loading TPM itself must be the last line in .tmux.conf, after every plugin declaration, since TPM reads the plugin list above it when it initializes.
Step 12: script tmux itself rather than only driving it by hand
tmux new -d -s build # create a detached session
tmux send-keys -t build 'make' Enter
tmux new-window -t build -n logs
tmux send-keys -t build:logs 'tail -f build.log' Enter
Every action available through tmux’s prefix-key bindings is also available as a plain tmux subcommand, which means an entire multi-window working layout can be set up non-interactively from a script — useful for a reproducible development environment that always starts with the same windows and commands already running, rather than manually recreating the same layout by hand every time. send-keys literally types the given keys into the target pane, exactly as if you’d typed them yourself, including the trailing Enter needed to actually execute the command rather than just populating the prompt with it.
Related:
- Fixing tmux Keybinding Conflicts With Your Terminal or Shell
- How to Use screen as a Session-Persistence Tool
Sources: