Why tmux?
Three problems tmux solves:
-
Session persistence โ SSH into a server, start a build, network drops. Without tmux: build dies. With tmux: it keeps running, you reconnect and attach.
-
Multiple panes โ split your terminal into panes. Run the server in one, logs in another, tests in a third. All in one window.
-
Session management โ named, persistent sessions for different projects. Switch between them like browser tabs.
Installation
# macOS
brew install tmux
# Ubuntu/Debian
sudo apt install tmux
# Fedora/RHEL
sudo dnf install tmux
Core Concepts
- Session โ a collection of windows. Persists even when you disconnect.
- Window โ like a browser tab. Contains panes.
- Pane โ a split region within a window.
All tmux commands start with the prefix key: Ctrl+B by default.
Essential Commands
Sessions
# Start a new named session
tmux new -s myproject
# List all sessions
tmux ls
# Attach to a session
tmux attach -t myproject
# or just:
tmux a -t myproject
# Detach from current session (keep it running)
Ctrl+B d
# Kill a session
tmux kill-session -t myproject
Panes (splitting)
Ctrl+B % โ Split horizontally (left/right)
Ctrl+B " โ Split vertically (top/bottom)
Ctrl+B โโโโ โ Navigate between panes
Ctrl+B z โ Zoom (maximize) current pane / unzoom
Ctrl+B x โ Close current pane
Windows
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 โ Switch to window by number
Ctrl+B w โ List all windows (interactive)
A Practical Workflow
# Start a session for your project
tmux new -s api-project
# Layout: server | editor | tests
Ctrl+B " # Split top/bottom
Ctrl+B % # Split bottom-left/bottom-right
# Pane 1 (top): server
npm run dev
# Navigate to pane 2 (bottom-left)
Ctrl+B โ
# Pane 2: editor or file watching
vim src/api.ts
# Navigate to pane 3 (bottom-right)
Ctrl+B โ
# Pane 3: tests
npm test -- --watch
# Detach โ everything keeps running
Ctrl+B d
# Come back later
tmux attach -t api-project
Customize tmux: ~/.tmux.conf
The default config is usable but not ideal. Recommended settings:
# ~/.tmux.conf
# Change prefix to Ctrl+A (screen-style, easier to type)
set -g prefix C-a
unbind C-b
bind C-a send-prefix
# Intuitive pane splits (| and -)
bind | split-window -h -c "#{pane_current_path}"
bind - split-window -v -c "#{pane_current_path}"
# Vim-style pane navigation
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R
# Enable mouse support
set -g mouse on
# Increase scrollback history
set -g history-limit 50000
# Start windows and panes at 1 (easier to reach on keyboard)
set -g base-index 1
setw -g pane-base-index 1
# Reload config with prefix + r
bind r source-file ~/.tmux.conf \; display "Config reloaded"
# True color support
set -g default-terminal "tmux-256color"
set -ag terminal-overrides ",xterm-256color:RGB"
Apply changes: Ctrl+B r (after adding the reload binding) or tmux source ~/.tmux.conf.
Copy Mode: Scrolling and Copying
Ctrl+B [ โ Enter copy mode (scroll freely with arrow keys / PageUp/Down)
q โ Exit copy mode
Ctrl+B ] โ Paste buffer
In copy mode with vi keys enabled:
# ~/.tmux.conf
setw -g mode-keys vi
v โ start selection
y โ yank (copy)
/ โ search forward
Key Takeaways
- Sessions persist after disconnect โ attach with
tmux attach -t name - Prefix key:
Ctrl+B(or remap toCtrl+A) %and"split panes; arrow keys navigatedto detach (session keeps running),$to rename session- Set
mouse onin.tmux.confโ scroll with mouse in any pane - Use named sessions per project โ
tmux new -s myproject