Catalog / tmux Cheatsheet

tmux Cheatsheet

A comprehensive guide to using tmux, the terminal multiplexer. This cheatsheet covers essential commands, session management, window and pane manipulation, and customization options to enhance your workflow.

Getting Started with tmux

Basic Concepts

What is tmux?
tmux is a terminal multiplexer: It allows you to run multiple terminal sessions within a single window. This is particularly useful for managing multiple programs from a single terminal, and for detaching and reattaching sessions.

Key Binding Prefix
Most tmux commands are prefixed with a key combination. By default, this is Ctrl+b (C-b). In this cheat sheet, C-b will be used to represent this prefix.

Starting and Ending Sessions

tmux

Starts a new tmux session.

tmux new -s <session_name>

Starts a new named session.

tmux attach

Attaches to the default session.

tmux attach -t <session_name>

Attaches to a specific session.

C-b d

Detaches the current session (leaves tmux running in the background).

exit or C-d

Closes the current pane/window. Closes the session if it’s the last one.

Basic Navigation

C-b ?

Shows the help screen with available key bindings.

C-b s

Lists all tmux sessions and allows switching between them.

tmux ls

Lists all active tmux sessions (from the command line).

Working with Windows and Panes

Window Management

C-b c

Creates a new window.

C-b ,

Renames the current window.

C-b n

Goes to the next window.

C-b p

Goes to the previous window.

C-b 0-9

Goes to the specified window number.

C-b w

Displays a list of windows for selection.

C-b &

Kills the current window (prompts for confirmation).

Pane Management

C-b "

Splits the current pane horizontally.

C-b %

Splits the current pane vertically.

C-b <arrow_key>

Moves to the pane in the direction of the arrow key (up, down, left, right).

C-b o

Moves to the next pane.

C-b {

Swaps the current pane with the previous pane.

C-b }

Swaps the current pane with the next pane.

C-b x

Kills the current pane (prompts for confirmation).

C-b !

Breaks the current pane out into a new window.

C-b q

Briefly displays pane numbers, allowing you to jump to a specific pane by pressing its number.

Advanced Features and Customization

Copy Mode and Scrolling

C-b [

Enters copy mode, allowing you to scroll and copy text.

C-b ]

Pastes the last copied text.

Space

In copy mode, starts text selection.

Enter

In copy mode, copies the selected text.

q

Exits copy mode.

C-s

In copy mode, enters search mode to find text within the buffer.

n/N

In copy/search mode, goes to the next/previous search result.

Configuration

tmux behavior can be configured through the ~/.tmux.conf file. This file is read when tmux starts.

Example Configuration:

# Set 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

# Set default terminal mode
set -g default-terminal "xterm-256color"

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

Useful settings for .tmux.conf

set -g mouse on

Enables mouse support for pane selection, resizing and scrolling.

set -g default-terminal "xterm-256color"

Sets the default terminal type for better color support.

bind r source-file ~/.tmux.conf \; display "~/.tmux.conf reloaded!"

Binds C-b r to reload the configuration file.

set -g status-position top

Positions the status bar at the top of the screen.

setw -g automatic-rename on

Automatically renames windows based on the running program.

set -g history-limit 5000

Increase the history limit

Status Bar Customization

Status Bar Options

set -g status-justify <left|center|right>

Justifies the status bar content.

set -g status-left '...'

Sets the content for the left side of the status bar.

set -g status-right '...'

Sets the content for the right side of the status bar.

set -g status-style 'bg=#333333 fg=#aaaaaa'

Sets the background and foreground colors for the entire status bar.

setw -g window-status-style 'fg=#bbbbbb bg=default'

Sets the style for inactive windows in the status bar.

setw -g window-status-current-style 'fg=#ffffff bg=#333333 bold'

Sets the style for the active window in the status bar.

Status Bar Variables

#(date)

Executes a shell command and displays its output.

#I

Displays the window index.

#S

Displays the session name.

#W

Displays the window name.

#H

Displays the hostname.

#T

Displays the pane title.

Status Bar Examples

Example Status Bar Configuration

set -g status-bg black
set -g status-fg white
set -g status-left '#[fg=green]Session: #S #[fg=yellow]#I #[fg=cyan]#P#[default]'
set -g status-right '#[fg=cyan]%Y-%m-%d %H:%M#[default]'

This configuration sets a black background and white foreground for the status bar, displays the session name, window index, and pane index on the left, and the current date and time on the right.