tmux
tmux#
Prerequisite — Terminal Multiplexing: Normally, if you close your terminal or lose your SSH connection, all processes in that terminal die. A terminal multiplexer like tmux solves this: it creates persistent sessions that survive disconnections. It also lets you split one terminal into multiple panes and windows. Think of tmux as a “window manager” for your terminal.
Concepts#
What Is tmux?#
tmux (terminal multiplexer) lets you:
- Persist sessions: detach from a session and reattach later (even after disconnecting SSH)
- Split the screen: multiple panes in one window
- Multiple windows: like browser tabs for your terminal
- Share sessions: multiple users can connect to the same session
Install tmux#
sudo apt install -y tmux
tmux Hierarchy#
Server
└── Session (named group of windows)
├── Window 0 (like a tab)
│ ├── Pane 0
│ └── Pane 1
└── Window 1
└── Pane 0
The Prefix Key#
tmux commands start with a prefix key: Ctrl + b (press and release, then the next key).
Written as: C-b then key
Session Management#
# Start a new session
tmux
tmux new -s mysession # with a name
# Detach from session
# C-b d
# List sessions
tmux ls
# Reattach
tmux attach -t mysession
tmux a # attach to last session
# Kill a session
tmux kill-session -t mysession
# Rename current session
# C-b $
Windows (Tabs)#
| Shortcut | Action |
|---|---|
C-b c |
Create new window |
C-b , |
Rename current window |
C-b n |
Next window |
C-b p |
Previous window |
C-b 0-9 |
Switch to window by number |
C-b w |
List all windows (interactive) |
C-b & |
Kill current window |
Panes (Splits)#
| Shortcut | Action |
|---|---|
C-b % |
Split horizontally (left/right) |
C-b " |
Split vertically (top/bottom) |
C-b arrow |
Move between panes |
C-b z |
Toggle pane zoom (fullscreen) |
C-b x |
Kill current pane |
C-b { |
Swap pane left |
C-b } |
Swap pane right |
C-b space |
Cycle pane layouts |
C-b C-arrow |
Resize pane |
Copy Mode (Scrollback)#
C-b [ enters copy mode. Use arrow keys or Page Up/Down to scroll. Press q to exit.
Basic .tmux.conf#
cat > ~/.tmux.conf << 'EOF'
# Remap prefix to Ctrl+a (easier to reach)
unbind C-b
set -g prefix C-a
bind C-a send-prefix
# Split panes with | and -
bind | split-window -h
bind - split-window -v
# Enable mouse support
set -g mouse on
# Start window numbering at 1
set -g base-index 1
setw -g pane-base-index 1
# Increase scrollback buffer
set -g history-limit 10000
# Status bar
set -g status-style bg=colour235,fg=white
EOF
Lab#
Exercise 1: Basic Session#
tmux new -s lab
# You're inside tmux. Run some commands:
echo "Inside tmux!"
# Detach: C-b d
# Back to normal terminal. Session is alive:
tmux ls
# Reattach:
tmux a -t lab
# Kill session:
exit
Exercise 2: Windows and Panes#
tmux new -s multi
# Create a second window: C-b c
# Switch back: C-b 0
# Split horizontally: C-b %
# Split vertically in one pane: C-b "
# Navigate between panes: C-b arrow keys
# Zoom a pane: C-b z (toggle)
# Type 'exit' in each pane to close them
Exercise 3: Survive Disconnection#
tmux new -s persistent
# Start a long-running command
watch -n 1 date
# Detach: C-b d
# The command keeps running!
tmux a -t persistent
# watch is still going. C-c to stop it.
exit
Review#
1. What is the main benefit of tmux?
Sessions persist when you disconnect. You can detach, close the terminal or SSH connection, and reattach later with everything still running.
2. What is the default prefix key?
Ctrl + b. All tmux shortcuts start with this prefix (press and release, then the next key).
3. How do you detach from a tmux session?
C-b d. The session continues running in the background.
4. How do you split the screen?
C-b % for horizontal split (left/right). C-b " for vertical split (top/bottom).
Previous: Keys, Config, and Tunnels | Next: Shell Customization