tmux lets you run multiple terminal sessions in one window, detach them, and reattach later. Essential for remote work.
Why tmux?
- Run long-running processes that survive disconnection
- Split terminal into multiple panes
- Multiple windows in one session
- Share sessions with others
- Scripted layouts
Installation
1
2
3
4
5
|
# Ubuntu/Debian
apt install tmux
# macOS
brew install tmux
|
Sessions
Create and Manage
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
# Start new session
tmux
# Named session
tmux new -s mysession
# List sessions
tmux ls
# Attach to session
tmux attach -t mysession
# Kill session
tmux kill-session -t mysession
|
Detach
Inside tmux, press: Ctrl+b d
Your session continues running. Reconnect anytime.
Key Bindings
All commands start with prefix: Ctrl+b
Session Commands
| Keys |
Action |
Ctrl+b d |
Detach |
Ctrl+b s |
List sessions |
Ctrl+b $ |
Rename session |
Window Commands
| Keys |
Action |
Ctrl+b c |
Create window |
Ctrl+b , |
Rename window |
Ctrl+b n |
Next window |
Ctrl+b p |
Previous window |
Ctrl+b 0-9 |
Switch to window |
Ctrl+b & |
Kill window |
Ctrl+b w |
List windows |
Pane Commands
| Keys |
Action |
Ctrl+b % |
Split vertical |
Ctrl+b " |
Split horizontal |
Ctrl+b o |
Cycle panes |
Ctrl+b arrows |
Navigate panes |
Ctrl+b x |
Kill pane |
Ctrl+b z |
Zoom pane (toggle) |
Ctrl+b { |
Move pane left |
Ctrl+b } |
Move pane right |
Ctrl+b space |
Cycle layouts |
Resize Panes
Ctrl+b then hold Ctrl + arrow keys
Or: Ctrl+b : then resize-pane -D 10 (down 10 rows)
Copy Mode
1
2
3
4
5
6
7
|
# Enter copy mode
Ctrl+b [
# Navigate with arrow keys or vim bindings
# Space to start selection
# Enter to copy
# Ctrl+b ] to paste
|
Configuration
Create ~/.tmux.conf:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
# Better prefix
set -g prefix C-a
unbind C-b
bind C-a send-prefix
# Mouse support
set -g mouse on
# Start windows at 1
set -g base-index 1
setw -g pane-base-index 1
# Faster escape
set -s escape-time 0
# Better splits
bind | split-window -h
bind - split-window -v
# Vim-like pane navigation
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R
# Easy reload
bind r source-file ~/.tmux.conf \; display "Reloaded!"
# Status bar
set -g status-style bg=black,fg=white
set -g status-left "[#S] "
set -g status-right "%H:%M"
|
Reload with: tmux source-file ~/.tmux.conf
Scripting Layouts
Create development environment:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
#!/bin/bash
# dev-session.sh
SESSION="dev"
tmux new-session -d -s $SESSION
# Main window with splits
tmux rename-window -t $SESSION:1 'main'
tmux send-keys -t $SESSION:1 'vim .' C-m
tmux split-window -h -t $SESSION:1
tmux send-keys -t $SESSION:1 'npm run dev' C-m
tmux split-window -v -t $SESSION:1
tmux send-keys -t $SESSION:1 'npm run test --watch' C-m
# Logs window
tmux new-window -t $SESSION:2 -n 'logs'
tmux send-keys -t $SESSION:2 'tail -f /var/log/app.log' C-m
# Select main window
tmux select-window -t $SESSION:1
tmux select-pane -t 0
tmux attach-session -t $SESSION
|
Common Workflows
Remote Server Work
1
2
3
4
5
6
7
|
# On server
tmux new -s work
# Do stuff
Ctrl+b d # Detach
# Later, reconnect
tmux attach -t work
|
Multiple Projects
1
2
3
4
5
6
7
8
9
10
11
|
tmux new -s project-a
# Work on project A
Ctrl+b d
tmux new -s project-b
# Work on project B
Ctrl+b d
# List and switch
tmux ls
tmux attach -t project-a
|
Pair Programming
1
2
3
4
5
6
7
8
|
# Person 1 creates session
tmux new -s pair
# Person 2 attaches (read-only)
tmux attach -t pair -r
# Or fully shared
tmux attach -t pair
|
Plugins (TPM)
Install Tmux Plugin Manager:
1
|
git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
|
In ~/.tmux.conf:
1
2
3
4
5
6
7
|
# List of plugins
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'
set -g @plugin 'tmux-plugins/tmux-resurrect' # Save/restore sessions
# Initialize TPM
run '~/.tmux/plugins/tpm/tpm'
|
Install with: Ctrl+b I
Quick Reference
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
# Session
tmux new -s name # New named session
tmux attach -t name # Attach to session
tmux ls # List sessions
Ctrl+b d # Detach
# Windows
Ctrl+b c # New window
Ctrl+b n/p # Next/prev window
Ctrl+b 1-9 # Jump to window
# Panes
Ctrl+b % # Vertical split
Ctrl+b " # Horizontal split
Ctrl+b arrows # Navigate
Ctrl+b z # Zoom toggle
Ctrl+b x # Kill pane
|
tmux transforms how you work in the terminal. The initial learning curve pays off quickly.
Comments