Terminal Multiplexers: tmux and screen
A terminal multiplexer lets you run multiple terminal sessions in a single window, detach and reattach to sessions remotely, and organize your work into windows and panes. tmux and GNU screen are the two dominant multiplexers on Linux and macOS.
tmux Basics
Sessions, Windows, and Panes
tmux
├── Session (e.g., "dev")
│ ├── Window 0: editor (e.g., vim)
│ │ ├── Pane 0: code
│ │ └── Pane 1: terminal
│ ├── Window 1: server
│ └── Window 2: logs| Level | Description | tmux command |
|---|---|---|
| Session | A collection of windows | tmux new -s myname |
| Window | A full-screen workspace | Prefix c (create) |
| Pane | A split within a window | Prefix % (vertical), Prefix " (horizontal) |
Essential Key Bindings
All tmux commands start with the prefix (default: Ctrl-b):
| Binding | Action |
|---|---|
Prefix % | Split pane vertically |
Prefix " | Split pane horizontally |
Prefix arrow | Navigate panes |
Prefix c | Create new window |
Prefix p / n | Previous / next window |
Prefix 0-9 | Switch to window by number |
Prefix d | Detach from session |
Prefix [ | Enter copy mode (scrollback) |
Prefix : | Enter command mode |
Starting and Reattaching
# Start a named session
tmux new -s development
# Detach: Ctrl-b d
# List sessions
tmux ls
# Reattach to an existing session
tmux attach -t development
# Kill a session
tmux kill-session -t developmentSession Management Flow
The typical development workflow with tmux:
- Start a session:
tmux new -s project-x - Create windows for editor, build, logs, and git
- Work across panes — split the editor window for code and test file side by side
- When you need to switch contexts or leave, detach:
Prefix d - SSH in from another machine and reattach:
tmux attach -t project-x - Everything is exactly where you left it — scrollback, split layout, cursor positions
tmux Configuration
Create ~/.tmux.conf to customize:
# Set prefix to Ctrl-a (like screen)
set -g prefix C-a
unbind C-b
bind C-a send-prefix
# Mouse support
set -g mouse on
# Increase scrollback buffer
set -g history-limit 50000
# Status bar customization
set -g status-bg black
set -g status-fg white
set -g status-left '#[fg=green]#S #[fg=white]|'
set -g status-right '#[fg=yellow]%Y-%m-%d %H:%M'
# Better pane navigation with vim keys
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R
# Reload config
bind r source-file ~/.tmux.conf \; display-message "Config reloaded"
# Use vim keys for copy mode
setw -g mode-keys vitmux Plugins with TPM
# ~/.tmux.conf — install TPM
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'
set -g @plugin 'tmux-plugins/tmux-resurrect'
set -g @plugin 'tmux-plugins/tmux-continuum'
run '~/.tmux/plugins/tpm/tpm'
# tmux-resurrect saves/restores sessions across reboots
set -g @resurrect-capture-pane-contents 'on'
# tmux-continuum auto-saves every 15 minutes
set -g @continuum-save-interval '15'The tmux-resurrect plugin is transformative: it saves the entire tmux state — windows, panes, working directories, and even program state (if using restore-shell-program) — and restores it after a reboot. tmux-continuum adds automatic saving, making session persistence nearly transparent.
Additional Useful Plugins
| Plugin | Purpose |
|---|---|
tmux-yank | System clipboard integration |
tmux-prefix-highlight | Shows when prefix is active |
tmux-battery | Battery status in status bar |
tmux-cpu | CPU usage in status bar |
tmux-open | Open highlighted text in browser |
GNU screen
Screen predates tmux and is available on virtually every Unix system. Its syntax and features overlap significantly with tmux but with some differences.
Screen Key Bindings
Prefix: Ctrl-a by default.
| Binding | Action |
|---|---|
Prefix c | Create new window |
Prefix S | Split horizontally |
Prefix Tab | Switch between regions |
Prefix d | Detach |
Prefix A | Title the current window |
Prefix esc | Enter copy/scrollback mode |
# Start a screen session
screen -S mysession
# List sessions
screen -ls
# Reattach
screen -r mysessionScreen Configuration (~/.screenrc)
# Status line
hardstatus alwayslastline
hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{=kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{Y}%Y-%m-%d %{w}%c %{g}]'
# Scrollback buffer size
defscrollback 10000
# Start with a window named
screen -t shell 0tmux vs screen
| Feature | tmux | screen |
|---|---|---|
| License | BSD | GPL |
| Configuration | More intuitive | More cryptic syntax |
| Split panes | Native, flexible | Regions (limited) |
| Copy mode | vim/emacs keys | Emacs by default |
| 256 color support | Excellent | Good |
| Plugin ecosystem | Rich (TPM, resurrect, continuum) | Minimal |
| Scriptable | send-keys, new-window | Similar, less documented |
| Default prefix | Ctrl-b | Ctrl-a (conflicts with bash line editing) |
tmux is the modern choice for new users. screen is valuable when you need something preinstalled on minimal systems or legacy servers.
Advanced tmux Workflows
Session Scripting
#!/bin/bash
# Automate a project setup
tmux new-session -d -s myproject
tmux rename-window -t myproject:0 'code'
tmux send-keys -t myproject:0 'cd ~/projects/myproject && vim' Enter
tmux new-window -t myproject:1 -n 'server'
tmux send-keys -t myproject:1 'cd ~/projects/myproject && npm run dev' Enter
tmux new-window -t myproject:2 -n 'git'
tmux send-keys -t myproject:2 'cd ~/projects/myproject && git status' Enter
tmux attach -t myprojectPane Synchronization
# Toggle synchronized input across all panes
bind S setw synchronize-panes \; display "Sync toggled"When synchronized, keystrokes in one pane are replicated to all panes in the same window — useful for running the same command across multiple servers.
Remote Session Attach
# From local machine, attach to a remote tmux session
ssh user@server -t 'tmux attach -t main || tmux new -s main'The -t flag forces SSH to allocate a TTY, and the fallback creates the session if it does not exist. Combined with tmux-resurrect, this makes remote development nearly indistinguishable from local work.
Custom Key Bindings for Efficiency
# Faster pane resizing
bind -r H resize-pane -L 5
bind -r J resize-pane -D 5
bind -r K resize-pane -U 5
bind -r L resize-pane -R 5
# Quick session switching
bind Tab switch-client -lThe -r flag allows the key to be repeated without holding the prefix, making resize operations fluid and fast.
FAQ
What is the main advantage of tmux over GNU screen?
tmux offers a better configuration syntax, native vertical and horizontal pane splitting, a rich plugin ecosystem (TPM), superior 256-color and true-color support, and more active development. screen’s main advantage is its near-universal availability.
How do I scroll back in tmux?
Enter copy mode with Prefix [ (default Ctrl+b [). Use PageUp/PageDown to scroll, or vi keys (j/k). Press q to exit copy mode.
Can I share a tmux session with another user?
Yes. Use tmux new-session -s shared and then tmux attach -t shared from another terminal. For multi-user sessions, use tmux new-session -s shared -d and others attach with tmux attach -t shared. The tmux -S /tmp/shared socket option allows session sharing between different Unix users.
How do I make tmux use my system clipboard?
Install the tmux-yank plugin, which integrates with xclip (Linux) or pbcopy (macOS). With mode-keys vi, select text in copy mode and press y to yank to the system clipboard.
Why does my tmux status bar show the wrong time?
The status bar updates by default every 15 seconds. You can change this with set -g status-interval 5. If the time zone is wrong, set the TZ environment variable before starting tmux.
Related: See our SSH keys guide and VS Code shortcuts.
Tmux vs Screen: A Detailed Comparison
While both tmux and GNU Screen provide terminal multiplexing, their feature sets differ significantly. Tmux offers a client-server architecture where sessions survive even if the terminal emulator crashes — reattach with tmux attach. Tmux’s vi/emacs mode keybindings are more intuitive for developers familiar with those editors. The status bar in tmux is highly customizable via status-left, status-right, and status-interval options, displaying battery, time, hostname, and session list. Tmux supports 256 colors and true color (24-bit) out of the box, while Screen requires extensive configuration. Tmux’s copy mode integrates with the system clipboard: set -g set-clipboard on enables seamless copy-paste between tmux and GUI applications. Screen has a smaller feature set but lighter resource usage, making it preferable on minimal embedded systems. Both support split panes, session persistence, and scrollback buffers, but tmux’s -S socket flag enables multiple users to share a session for pair programming.
Advanced Tmux Workflows
Tmux plugins via tpm (Tmux Plugin Manager) extend functionality: tmux-resurrect restores all panes, windows, and running programs after a reboot; tmux-yank adds system-clipboard integration; tmux-cpu displays CPU and memory usage. Nested tmux sessions (SSH into a server running tmux) require prefix key differentiation. Synchronized panes (setw synchronize-panes) type the same input into multiple panes simultaneously for mass server administration. Tmux’s pipe-pane logs pane output to a file, creating an audit trail.