Post

tmux - Terminal Multiplexer Guide

tmux - Terminal Multiplexer Guide

When working over SSH, one of the most frustrating experiences is a dropped connection killing a long-running process. tmux (terminal multiplexer) solves this - and much more. It lets you run persistent sessions, split your terminal into panes, and detach/reattach from anywhere.


Why tmux Over SSH

Without tmux, every SSH session is tied to your connection. If the network drops, the process dies. With tmux:

  • Sessions persist on the remote machine even after you disconnect
  • Re-attach from any client, any time
  • Multiple windows and panes in a single SSH connection - no need to open extra terminals
  • Run long builds, downloads, or scripts without babysitting the connection

Installation

1
2
# Debian / Ubuntu
sudo apt install tmux

Core Concepts

ConceptDescription
SessionA top-level container that persists independently of your terminal
WindowA full-screen tab inside a session
PaneA split subdivision within a window

The default prefix key is Ctrl+b - press it before every tmux command.


Session Management

Start a Named Session

1
tmux new -s mysession

Always name your sessions - it makes reattaching much easier.

Detach from a Session

1
Ctrl+b  d

The session keeps running in the background. You can now safely close the SSH connection.

List All Sessions

1
tmux ls

Reattach to a Session

1
tmux attach -t mysession

If there is only one session running:

1
tmux attach

Kill a Session

1
tmux kill-session -t mysession

Typical SSH Workflow

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[Local Machine] ──SSH──> [Remote Server]
                              │
                         tmux new -s work
                              │
                    ┌─────────┴──────────┐
                    │   long build run   │
                    │  (safe to detach)  │
                    └────────────────────┘
                              │
                       Ctrl+b  d  (detach)
                              │
              [SSH connection drops  -  no problem]
                              │
                    [Reconnect later via SSH]
                              │
                   tmux attach -t work

This is the core use case: start a job, detach, close your laptop, come back the next day and reattach.


Window Management

ActionKeys
New windowCtrl+b c
Next windowCtrl+b n
Previous windowCtrl+b p
Switch by numberCtrl+b 0-9
Rename windowCtrl+b ,
Close windowCtrl+b &

Pane Management

Split the current window to run multiple things side-by-side in one SSH connection:

ActionKeys
Split horizontally (left/right)Ctrl+b %
Split vertically (top/bottom)Ctrl+b "
Navigate between panesCtrl+b Arrow keys
Resize paneCtrl+b Ctrl+Arrow keys
Close paneCtrl+b x
Zoom pane (toggle fullscreen)Ctrl+b z

Example: monitor a log in one pane while editing in another, all over a single SSH connection:

1
2
3
4
5
┌──────────────────┬──────────────────┐
│                  │                  │
│   vim main.c     │  tail -f app.log │
│                  │                  │
└──────────────────┴──────────────────┘

Copy Mode (Scrollback)

SSH terminals often have limited scrollback. tmux has its own scrollback buffer:

1
2
3
Ctrl+b  [        # Enter copy mode
Arrow keys        # Scroll up/down
q                 # Exit copy mode

To scroll with the mouse wheel, add this to ~/.tmux.conf:

1
set -g mouse on

Useful Configuration (~/.tmux.conf)

1
2
3
4
5
6
7
8
9
10
11
12
# Enable mouse support
set -g mouse on

# Start window and pane numbering from 1 (easier to reach on keyboard)
set -g base-index 1
set -g pane-base-index 1

# Increase scrollback buffer
set -g history-limit 10000

# Reload config without restarting
bind r source-file ~/.tmux.conf \; display "Config reloaded"

Apply changes to a running session:

1
2
tmux source-file ~/.tmux.conf
# or inside tmux: Ctrl+b  r  (if you added the bind above)

This post is licensed under CC BY 4.0 by the author.