Linux tmux: Terminal Multiplexer Guide
tmux (terminal multiplexer) is a command-line tool that allows you to run multiple terminal sessions within a single window. More importantly, it keeps those sessions running in the background even...
Key Insights
- tmux creates persistent terminal sessions that survive disconnections, making it essential for remote server work and long-running processes
- The three-level hierarchy (sessions > windows > panes) provides powerful workspace organization that eliminates the need for multiple terminal emulators
- Investing 30 minutes to learn core tmux commands will save hours of context switching and window management over your career
Introduction to tmux
tmux (terminal multiplexer) is a command-line tool that allows you to run multiple terminal sessions within a single window. More importantly, it keeps those sessions running in the background even when you disconnect, making it indispensable for remote server administration and development work.
The killer feature isn’t just splitting your terminal—it’s session persistence. Start a long-running build, disconnect from your SSH session, reconnect hours later, and everything is exactly where you left it. No more lost work from dropped connections or accidentally closed terminals.
Installation is straightforward across major Linux distributions:
# Ubuntu/Debian
sudo apt update && sudo apt install tmux
# RHEL/CentOS/Fedora
sudo dnf install tmux
# Arch Linux
sudo pacman -S tmux
# Verify installation
tmux -V
Start your first session with a simple command:
# Start a new session
tmux
# Start a named session (recommended)
tmux new -s development
You’re now inside tmux. Press Ctrl+b then d to detach. Your session keeps running. Reattach with tmux attach -t development.
Core Concepts: Sessions, Windows, and Panes
tmux operates on a three-level hierarchy that maps naturally to how developers work:
Session (project/context)
├── Window 1 (task/component)
│ ├── Pane 1
│ └── Pane 2
├── Window 2
│ ├── Pane 1
│ ├── Pane 2
│ └── Pane 3
└── Window 3
└── Pane 1
Sessions are independent workspaces. Create one per project or major context. They persist until explicitly killed, surviving disconnections and system restarts (with tmux-resurrect plugin).
Windows are like tabs in a browser—different views within the same session. Use them for different aspects of your project: one for editing code, one for running tests, one for monitoring logs.
Panes are split views within a window. Use them when you need to see multiple things simultaneously.
Here’s how to work with each level:
# Session management
tmux new -s backend # Create named session
tmux ls # List all sessions
tmux attach -t backend # Attach to session
tmux kill-session -t backend # Kill session
# Window management (inside tmux, use Ctrl+b prefix)
Ctrl+b c # Create new window
Ctrl+b , # Rename current window
Ctrl+b n # Next window
Ctrl+b p # Previous window
Ctrl+b 0-9 # Jump to window number
# Pane management
Ctrl+b % # Split vertically
Ctrl+b " # Split horizontally
Ctrl+b arrow # Navigate between panes
Ctrl+b x # Kill current pane
Ctrl+b z # Zoom/unzoom pane (fullscreen toggle)
Essential tmux Commands and Key Bindings
tmux uses a prefix key (default Ctrl+b) followed by a command key. Think of it as a namespace that prevents conflicts with your shell or applications. You press Ctrl+b, release, then press the command key.
There are two ways to interact with tmux: key bindings for speed, and command mode for precision.
# Key binding example (quick)
Ctrl+b c # Create window
# Command mode example (explicit)
Ctrl+b : # Enter command mode
new-window -n "logs" # Create window named "logs"
Here’s your essential command reference:
| Action | Key Binding | Command Mode |
|---|---|---|
| Detach session | Ctrl+b d |
detach |
| List sessions | Ctrl+b s |
list-sessions |
| New window | Ctrl+b c |
new-window |
| Rename window | Ctrl+b , |
rename-window |
| Split vertical | Ctrl+b % |
split-window -h |
| Split horizontal | Ctrl+b " |
split-window -v |
| Navigate panes | Ctrl+b arrow |
- |
| Resize pane | Ctrl+b Ctrl+arrow |
resize-pane -D/U/L/R |
| Toggle zoom | Ctrl+b z |
resize-pane -Z |
| Kill pane | Ctrl+b x |
kill-pane |
| Next window | Ctrl+b n |
next-window |
| Previous window | Ctrl+b p |
previous-window |
| Choose window | Ctrl+b w |
choose-window |
| Command mode | Ctrl+b : |
- |
| Copy mode | Ctrl+b [ |
- |
| Paste buffer | Ctrl+b ] |
- |
For resizing panes, hold Ctrl+b then press Ctrl+arrow repeatedly. Or enter command mode and use explicit sizes:
Ctrl+b :
resize-pane -D 10 # Down 10 lines
resize-pane -R 20 # Right 20 columns
Practical Workflows and Use Cases
Let’s build real working environments. Here’s a typical web development setup:
# Create development session
tmux new -s webapp
# Window 1: Editor (already created)
# Rename it
Ctrl+b ,
# Type: editor
# Window 2: Server
Ctrl+b c
Ctrl+b ,
# Type: server
npm run dev
# Window 3: Logs and testing
Ctrl+b c
Ctrl+b ,
# Type: testing
Ctrl+b " # Split horizontal
# Top pane: watch tests
npm run test:watch
Ctrl+b arrow # Move to bottom pane
# Bottom pane: tail logs
tail -f logs/app.log
For server monitoring, create a dashboard layout:
# Monitoring session
tmux new -s monitor
# Split into 4 panes
Ctrl+b " # Split horizontal
Ctrl+b % # Split right pane vertical
Ctrl+b arrow-up # Move to top pane
Ctrl+b % # Split top pane vertical
# Now populate each pane
# Top-left: System resources
htop
# Top-right: Disk usage
watch -n 5 df -h
# Bottom-left: Network
watch -n 2 netstat -tuln
# Bottom-right: Application logs
tail -f /var/log/nginx/access.log
For pair programming, share a session:
# Host creates session
tmux -S /tmp/pair new -s pairing
chmod 777 /tmp/pair
# Guest attaches
tmux -S /tmp/pair attach -t pairing
Both users see and control the same session. Perfect for remote collaboration.
Customization and Configuration
tmux reads ~/.tmux.conf on startup. Here’s a practical configuration:
# ~/.tmux.conf
# Change prefix from Ctrl+b to Ctrl+a (easier to reach)
unbind C-b
set-option -g prefix C-a
bind-key C-a send-prefix
# Split panes using | and - (more intuitive)
unbind '"'
unbind %
bind | split-window -h
bind - split-window -v
# Enable mouse support (scroll, select panes, resize)
set -g mouse on
# Start window/pane numbering at 1 (not 0)
set -g base-index 1
setw -g pane-base-index 1
# Renumber windows when one is closed
set -g renumber-windows on
# Increase scrollback buffer
set -g history-limit 10000
# Faster command sequences (no delay)
set -s escape-time 0
# Status bar customization
set -g status-style bg=black,fg=white
set -g status-left '[#S] '
set -g status-right '%Y-%m-%d %H:%M'
set -g status-left-length 20
# Highlight active window
setw -g window-status-current-style bg=red,fg=white,bold
# Pane border colors
set -g pane-border-style fg=colour240
set -g pane-active-border-style fg=colour33
# Vi mode for copy mode
setw -g mode-keys vi
bind-key -T copy-mode-vi v send-keys -X begin-selection
bind-key -T copy-mode-vi y send-keys -X copy-selection-and-cancel
# Reload config quickly
bind r source-file ~/.tmux.conf \; display "Config reloaded!"
After editing, reload with tmux source-file ~/.tmux.conf or use the reload binding (Ctrl+a r with the config above).
Advanced Features and Tips
Automate session creation with shell scripts:
#!/bin/bash
# ~/bin/tmux-dev
SESSION="myproject"
# Create session detached
tmux new-session -d -s $SESSION -n editor
# Window 1: Editor
tmux send-keys -t $SESSION:editor "cd ~/projects/myproject && vim" C-m
# Window 2: Server
tmux new-window -t $SESSION -n server
tmux send-keys -t $SESSION:server "cd ~/projects/myproject && npm run dev" C-m
# Window 3: Git
tmux new-window -t $SESSION -n git
tmux send-keys -t $SESSION:git "cd ~/projects/myproject && git status" C-m
# Attach to session
tmux attach -t $SESSION
Make it executable: chmod +x ~/bin/tmux-dev
For plugin management, use TPM (tmux Plugin Manager):
# Install TPM
git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
# Add to ~/.tmux.conf
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'
set -g @plugin 'tmux-plugins/tmux-resurrect' # Save/restore sessions
set -g @plugin 'tmux-plugins/tmux-continuum' # Auto-save sessions
# Initialize TPM (keep at bottom of tmux.conf)
run '~/.tmux/plugins/tpm/tpm'
Install plugins: Ctrl+b I (capital I)
Conclusion and Quick Reference
tmux transforms how you interact with the command line. The learning curve is real but short—master these basics and you’ll wonder how you ever worked without it.
Quick Start Cheat Sheet:
# Session basics
tmux new -s name # Create session
tmux ls # List sessions
tmux a -t name # Attach to session
Ctrl+b d # Detach
# Windows (tabs)
Ctrl+b c # Create window
Ctrl+b , # Rename window
Ctrl+b n/p # Next/previous window
Ctrl+b 0-9 # Jump to window
# Panes (splits)
Ctrl+b % # Vertical split
Ctrl+b " # Horizontal split
Ctrl+b arrow # Navigate panes
Ctrl+b z # Zoom pane
Ctrl+b x # Kill pane
# Essential
Ctrl+b ? # List all bindings
Ctrl+b : # Command mode
Start with one session per project, use windows for different tasks, and split panes when you need side-by-side views. Customize your prefix key and status bar. Within a week, tmux will feel natural. Within a month, you won’t be able to work without it.