Freebie

tmux

tmux is a terminal multiplexer: it lets you run multiple terminal sessions inside a single window, split that window into panes, and detach from sessions without killing them. The session persists on the server until you explicitly destroy it—even after you close your terminal or lose an SSH connection.

Installation

Install tmux from your distribution’s package manager:

# Debian/Ubuntu
sudo apt install tmux

# RHEL/Fedora
sudo dnf install tmux

Confirm the installed version:

tmux -V

Core concepts

tmux organizes work into three layers:

LayerDescription
SessionA collection of windows. You attach to and detach from sessions.
WindowA single screen within a session. Equivalent to a browser tab.
PaneA rectangular division of a window. Each pane runs its own shell.

A tmux server runs in the background and manages all sessions. When you run tmux, it starts the server (if not already running) and creates your first session.

Session management

Start a new named session:

tmux new-session -s mysession

List active sessions:

tmux ls

Attach to a session by name:

tmux attach-session -t mysession
Key bindingAction
Prefix dDetach from the current session
Prefix sOpen the interactive session list
Prefix $Rename the current session
Prefix (Switch to the previous session
Prefix )Switch to the next session

Kill a session from the command line:

tmux kill-session -t mysession

Window management

Key bindingAction
Prefix cCreate a new window
Prefix nMove to the next window
Prefix pMove to the previous window
Prefix 0–9Jump to a window by its index number
Prefix ,Rename the current window
Prefix wOpen the interactive window list
Prefix &Close the current window (prompts for confirmation)

Pane management

Key bindingAction
Prefix %Split the current pane vertically (side by side)
Prefix "Split the current pane horizontally (top and bottom)
Prefix ←→↑↓Move focus to the pane in that direction
Prefix zZoom the current pane to fill the window; press again to unzoom
Prefix xClose the current pane (prompts for confirmation)
Prefix {Swap the current pane with the one above it
Prefix }Swap the current pane with the one below it
Prefix qDisplay pane index numbers briefly
Prefix Ctrl+←→↑↓Resize the current pane one cell at a time
Prefix SpaceCycle through the built-in pane layouts

Key bindings

The prefix key is the key combination you press before every tmux command. The default is Ctrl+b. To send a literal Ctrl+b to the running program, press it twice.

Two bindings are useful at any time:

Key bindingAction
Prefix ?List all current key bindings
Prefix :Open the tmux command prompt

Copy mode

Copy mode lets you scroll through terminal output and copy text without a mouse. tmux supports vi key bindings in copy mode.

Enable vi keys in ~/.tmux.conf:

set-window-option -g mode-keys vi

Enter and exit copy mode:

Key bindingAction
Prefix [Enter copy mode
qExit copy mode

Navigate in copy mode using standard vi motion keys:

KeyAction
h j k lMove left, down, up, right
w / bJump forward / backward one word
gg / GJump to the top / bottom of the scrollback buffer
/Search forward
?Search backward
n / NJump to the next / previous search match

Select and copy text:

KeyAction
SpaceStart a selection
EnterCopy the selection and exit copy mode

Paste the copied text:

Key bindingAction
Prefix ]Paste the most recent buffer

.tmux.conf

tmux reads ~/.tmux.conf at startup. Changes to this file do not apply to running sessions automatically.

Apply changes to the current session without restarting:

tmux source-file ~/.tmux.conf

Or from within tmux: Prefix : then source-file ~/.tmux.conf.

Common configuration options:

# Remap the prefix to Ctrl+a
unbind C-b
set -g prefix C-a
bind C-a send-prefix

# Enable mouse support
set -g mouse on

# Increase scrollback buffer
set -g history-limit 10000

# Start window and pane indices at 1 instead of 0
set -g base-index 1
setw -g pane-base-index 1

# Use vi keys in copy mode
setw -g mode-keys vi

# Remove the escape-key delay (important for vim users)
set -s escape-time 0

Status bar customization

The status bar runs along the bottom of the tmux window. You can change its colors and content.

# Status bar colors
set -g status-bg colour235
set -g status-fg white

# Left and right content
set -g status-left "[#S] "
set -g status-right "#H | %Y-%m-%d %H:%M"

# Refresh interval in seconds
set -g status-interval 5

Common status bar variables:

VariableValue
#SSession name
#WCurrent window name
#HHostname
#(cmd)Output of a shell command

Plugins

tmux Plugin Manager (TPM) installs and manages tmux plugins from GitHub.

Install TPM:

git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm

Add the following to the bottom of ~/.tmux.conf:

set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'

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

Reload your config, then install the plugins:

tmux source-file ~/.tmux.conf

Then from within tmux:

Key bindingAction
Prefix IInstall plugins
Prefix UUpdate plugins
Prefix Alt+uRemove unused plugins

Recommended plugins:

PluginPurpose
tmux-plugins/tmux-sensibleA set of sane defaults that most users agree on
tmux-plugins/tmux-resurrectSave and restore sessions across reboots
tmux-plugins/tmux-continuumAutomatically save sessions on an interval
tmux-plugins/tmux-yankCopy to the system clipboard from copy mode

To add a plugin, append a set -g @plugin line for it and press Prefix I.

Scripting with tmux

You can drive tmux non-interactively from shell scripts.

Create a detached session:

tmux new-session -d -s mysession

Check whether a session exists before creating it:

if ! tmux has-session -t mysession 2>/dev/null; then
    tmux new-session -d -s mysession
fi

Send a command to a session:

tmux send-keys -t mysession "htop" Enter

This pattern is useful for provisioning a standard workspace on login or in a dotfiles script.

The following ~/.tmux.conf consolidates the settings from the sections above into a working starting configuration. Copy it as-is, then adjust to taste.

Install TPM first:

git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm

Then write ~/.tmux.conf:

# Prefix
unbind C-b
set -g prefix C-a
bind C-a send-prefix

# General
set -g mouse on
set -g history-limit 10000
set -s escape-time 0

# Indexing: start windows and panes at 1
set -g base-index 1
setw -g pane-base-index 1

# Copy mode
setw -g mode-keys vi

# Status bar
set -g status-bg colour235
set -g status-fg white
set -g status-left "[#S] "
set -g status-right "#H | %Y-%m-%d %H:%M"
set -g status-interval 5

# Plugins
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'
set -g @plugin 'tmux-plugins/tmux-resurrect'
set -g @plugin 'tmux-plugins/tmux-yank'

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

Apply the config and install the plugins:

tmux source-file ~/.tmux.conf

Then from within tmux, press Prefix I to install the plugins.