SSH into a server, start a long-running process, lose connection, lose everything. tmux solves this by keeping sessions alive independently of your terminal.

Why tmux?

  • Persistence: Sessions survive disconnections
  • Multiplexing: Multiple windows and panes in one terminal
  • Remote pairing: Share sessions with teammates
  • Scriptable: Automate complex layouts

Basic Concepts

tmuxSSeessssiWWioiionnnndd(oPPo2nwaawannm1ee2ed(12lciokleleacttiaobn)ofwindows)

Getting Started

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# Start new session
tmux

# Start named session
tmux new -s myproject

# List sessions
tmux ls

# Attach to session
tmux attach -t myproject

# Attach to last session
tmux attach

The Prefix Key

All tmux commands start with a prefix (default: Ctrl+b), then a key.

I’ll write it as prefix + key. So prefix + d means: press Ctrl+b, release, press d.

Essential Commands

Session Management

CommandAction
prefix + dDetach from session
prefix + sList sessions
prefix + $Rename session
prefix + (Previous session
prefix + )Next session

Windows (Tabs)

CommandAction
prefix + cCreate window
prefix + nNext window
prefix + pPrevious window
prefix + 0-9Go to window N
prefix + ,Rename window
prefix + &Kill window
prefix + wList windows

Panes (Splits)

CommandAction
prefix + %Split vertically
prefix + "Split horizontally
prefix + arrowsNavigate panes
prefix + zToggle pane zoom
prefix + xKill pane
prefix + oCycle panes
prefix + spaceCycle layouts

Practical Workflow

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# Start a development session
tmux new -s dev

# Create layout:
# - Window 0: Editor (vim)
# - Window 1: Server (running app)
# - Window 2: Git/misc

# In window 0, split for editor + terminal
prefix + "    # horizontal split
prefix + %    # vertical split in bottom pane

# Create more windows
prefix + c    # new window for server
prefix + c    # new window for git

# Rename windows
prefix + ,    # type "editor"
prefix + n    # go to next
prefix + ,    # type "server"

Copy Mode

tmux has a built-in copy mode for scrolling and copying text.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
prefix + [    # Enter copy mode
q             # Exit copy mode

# In copy mode (vi keys):
j/k           # Scroll down/up
Ctrl+d/u      # Page down/up
/             # Search forward
?             # Search backward
space         # Start selection
enter         # Copy selection

prefix + ]    # 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
33
34
35
# Better prefix (Ctrl+a instead of Ctrl+b)
unbind C-b
set -g prefix C-a
bind C-a send-prefix

# Start windows at 1, not 0
set -g base-index 1
setw -g pane-base-index 1

# Enable mouse
set -g mouse on

# Vim-style pane navigation
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R

# Split with | and -
bind | split-window -h -c "#{pane_current_path}"
bind - split-window -v -c "#{pane_current_path}"

# Reload config
bind r source-file ~/.tmux.conf \; display "Reloaded!"

# Increase history
set -g history-limit 10000

# Better colors
set -g default-terminal "screen-256color"

# Status bar
set -g status-style bg=black,fg=white
set -g status-left '[#S] '
set -g status-right '%H:%M %d-%b'

Reload with prefix + r or tmux source-file ~/.tmux.conf.

Remote Persistence

The killer feature: start something, detach, disconnect, reconnect later.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Start long task
tmux new -s backup
./run-backup.sh

# Detach
prefix + d

# Disconnect SSH, go home, reconnect
ssh server
tmux attach -t backup
# Your backup is still running!

Scripting Sessions

Create complex layouts programmatically:

 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
#!/bin/bash
# setup-dev.sh

SESSION="dev"

# Create session
tmux new-session -d -s $SESSION

# Window 0: Editor
tmux rename-window -t $SESSION:0 'editor'
tmux send-keys -t $SESSION:0 'vim .' C-m

# Window 1: Server
tmux new-window -t $SESSION -n 'server'
tmux send-keys -t $SESSION:1 'npm run dev' C-m

# Window 2: Git
tmux new-window -t $SESSION -n 'git'
tmux send-keys -t $SESSION:2 'git status' C-m

# Window 3: Logs (split panes)
tmux new-window -t $SESSION -n 'logs'
tmux split-window -h -t $SESSION:3
tmux send-keys -t $SESSION:3.0 'tail -f /var/log/app.log' C-m
tmux send-keys -t $SESSION:3.1 'tail -f /var/log/error.log' C-m

# Attach to session
tmux attach -t $SESSION

Session Sharing

Pair programming with shared tmux:

1
2
3
4
5
6
7
# Person 1: Start session
tmux new -s pairing

# Person 2: Attach to same session
tmux attach -t pairing

# Both see the same thing, both can type

For read-only:

1
tmux attach -t pairing -r

Useful Plugins

With TPM (tmux plugin manager):

1
2
3
4
5
6
7
# In ~/.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

run '~/.tmux/plugins/tpm/tpm'

tmux-resurrect saves your session layout and restores it after reboot. Game changer.

Quick Reference

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# Sessions
tmux new -s name      # New named session
tmux ls               # List sessions
tmux attach -t name   # Attach to session
tmux kill-session -t name

# From inside tmux
prefix + d            # Detach
prefix + s            # Session list
prefix + c            # New window
prefix + n/p          # Next/prev window
prefix + %            # Vertical split
prefix + "            # Horizontal split
prefix + arrows       # Navigate panes
prefix + z            # Zoom pane
prefix + [            # Copy mode
prefix + ?            # Help (all bindings)

tmux transforms how you work with remote servers. Once you’re used to persistent sessions and pane layouts, you’ll wonder how you ever worked without it.