Skip to content

tmux Cheatsheet

Overview

tmux (terminal multiplexer) is a tool that allows you to run multiple terminal sessions within a single terminal window. It enables you to detach and reattach sessions, making it perfect for remote work and long-running processes.

Installation

Package Managers

bash
# Ubuntu/Debian
sudo apt install tmux

# macOS
brew install tmux

# CentOS/RHEL
sudo yum install tmux

# Arch Linux
sudo pacman -S tmux

# From source
git clone https://github.com/tmux/tmux.git
cd tmux && ./configure && make && sudo make install

Basic Concepts

Terminology

Session    # A collection of windows
Window     # A single terminal screen
Pane       # A subdivision of a window
Prefix     # Default Ctrl+b (customizable)

Session Hierarchy

Session
├── Window 1
│   ├── Pane 1
│   └── Pane 2
├── Window 2
│   └── Pane 1
└── Window 3
    ├── Pane 1
    ├── Pane 2
    └── Pane 3

Basic Usage

Starting tmux

bash
# Start new session
tmux

# Start named session
tmux new-session -s mysession
tmux new -s mysession

# Start session with specific window name
tmux new -s mysession -n mywindow

# Start detached session
tmux new -d -s mysession

Session Management

bash
# List sessions
tmux list-sessions
tmux ls

# Attach to session
tmux attach-session -t mysession
tmux attach -t mysession
tmux a -t mysession

# Detach from session
# Inside tmux: Ctrl+b d

# Kill session
tmux kill-session -t mysession

# Kill all sessions
tmux kill-server

Key Bindings (Default Prefix: Ctrl+b)

Session Commands

Prefix + d    # Detach from session
Prefix + s    # List sessions
Prefix + $    # Rename session
Prefix + (    # Previous session
Prefix + )    # Next session

Window Commands

Prefix + c    # Create new window
Prefix + n    # Next window
Prefix + p    # Previous window
Prefix + l    # Last window
Prefix + w    # List windows
Prefix + &    # Kill window
Prefix + ,    # Rename window
Prefix + 0-9  # Switch to window by number
Prefix + f    # Find window

Pane Commands

Prefix + %    # Split horizontally
Prefix + "    # Split vertically
Prefix + o    # Switch to next pane
Prefix + ;    # Switch to last pane
Prefix + x    # Kill pane
Prefix + !    # Break pane into window
Prefix + z    # Toggle pane zoom
Prefix + {    # Move pane left
Prefix + }    # Move pane right
Prefix + q    # Show pane numbers
Prefix + q 0-9 # Switch to pane by number

Pane Navigation

Prefix + ↑    # Move to pane above
Prefix + ↓    # Move to pane below
Prefix + ←    # Move to pane left
Prefix + →    # Move to pane right

Pane Resizing

Prefix + Ctrl+↑  # Resize pane up
Prefix + Ctrl+↓  # Resize pane down
Prefix + Ctrl+←  # Resize pane left
Prefix + Ctrl+→  # Resize pane right

# Alternative (hold Prefix)
Prefix + Alt+↑   # Resize pane up (5 lines)
Prefix + Alt+↓   # Resize pane down (5 lines)
Prefix + Alt+←   # Resize pane left (5 columns)
Prefix + Alt+→   # Resize pane right (5 columns)

Copy Mode

Enter Copy Mode

Prefix + [    # Enter copy mode

Copy Mode Navigation

h, j, k, l    # Move cursor (vim-style)
Arrow keys    # Move cursor
w             # Next word
b             # Previous word
0             # Beginning of line
$             # End of line
g             # Go to top
G             # Go to bottom
Ctrl+f        # Page down
Ctrl+b        # Page up
/             # Search forward
?             # Search backward
n             # Next search result
N             # Previous search result

Copy Mode Selection

Space         # Start selection
Enter         # Copy selection and exit
Escape        # Exit copy mode
v             # Start selection (vi mode)
y             # Copy selection (vi mode)

Paste

Prefix + ]    # Paste buffer

Configuration

Configuration File (~/.tmux.conf)

bash
# Basic settings
set -g default-terminal "screen-256color"
set -g history-limit 10000
set -g base-index 1
setw -g pane-base-index 1

# Change prefix key
unbind C-b
set -g prefix C-a
bind C-a send-prefix

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

# Mouse support
set -g mouse on

# Vi mode
setw -g mode-keys vi

# Pane splitting
bind | split-window -h
bind - split-window -v
unbind '"'
unbind %

# Pane navigation (vim-style)
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R

# Window navigation
bind -n M-Left previous-window
bind -n M-Right next-window

# Pane resizing
bind -r H resize-pane -L 5
bind -r J resize-pane -D 5
bind -r K resize-pane -U 5
bind -r L resize-pane -R 5

# Status bar
set -g status-bg black
set -g status-fg white
set -g status-left '#[fg=green]#S '
set -g status-right '#[fg=yellow]%Y-%m-%d %H:%M'
set -g status-interval 60

# Window status
setw -g window-status-current-style 'fg=black bg=white'
setw -g window-status-style 'fg=white bg=black'

# Pane borders
set -g pane-border-style 'fg=colour238'
set -g pane-active-border-style 'fg=colour51'

Advanced Configuration

bash
# Automatic window renaming
setw -g automatic-rename on
set -g set-titles on

# Activity monitoring
setw -g monitor-activity on
set -g visual-activity on

# Copy mode improvements
bind -T copy-mode-vi v send-keys -X begin-selection
bind -T copy-mode-vi y send-keys -X copy-pipe-and-cancel 'xclip -in -selection clipboard'
bind -T copy-mode-vi r send-keys -X rectangle-toggle

# Synchronize panes
bind S setw synchronize-panes

# Quick pane cycling
bind -n M-o select-pane -t :.+

# Session management
bind C-s choose-session
bind C-n new-session

Advanced Features

Session Scripting

bash
# Create session with multiple windows
tmux new-session -d -s development
tmux new-window -t development -n editor 'vim'
tmux new-window -t development -n server 'cd ~/project && npm start'
tmux split-window -t development:server -h 'cd ~/project && npm run watch'
tmux attach-session -t development

Tmux Scripts

bash
#!/bin/bash
# dev-session.sh

SESSION="development"

# Check if session exists
tmux has-session -t $SESSION 2>/dev/null

if [ $? != 0 ]; then
    # Create new session
    tmux new-session -d -s $SESSION
    
    # Setup windows
    tmux rename-window -t $SESSION:1 'editor'
    tmux send-keys -t $SESSION:1 'cd ~/project && vim' C-m
    
    tmux new-window -t $SESSION -n 'server'
    tmux send-keys -t $SESSION:2 'cd ~/project && npm start' C-m
    
    tmux new-window -t $SESSION -n 'git'
    tmux send-keys -t $SESSION:3 'cd ~/project && git status' C-m
    
    # Split server window
    tmux split-window -t $SESSION:2 -h
    tmux send-keys -t $SESSION:2.2 'cd ~/project && npm run watch' C-m
fi

# Attach to session
tmux attach-session -t $SESSION

Tmuxinator

bash
# Install tmuxinator
gem install tmuxinator

# Create project
tmuxinator new myproject

# Example tmuxinator config (~/.tmuxinator/myproject.yml)
name: myproject
root: ~/projects/myproject

windows:
  - editor:
      layout: main-vertical
      panes:
        - vim
        - guard
  - server: bundle exec rails s
  - logs: tail -f log/development.log

Plugins

TPM (Tmux Plugin Manager)

bash
# Install TPM
git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm

# Add to ~/.tmux.conf
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-continuum'

# Initialize TPM (add to bottom of .tmux.conf)
run '~/.tmux/plugins/tpm/tpm'

# Install plugins: Prefix + I
# Update plugins: Prefix + U
# Remove plugins: Prefix + alt + u
bash
# tmux-resurrect - save/restore sessions
set -g @plugin 'tmux-plugins/tmux-resurrect'
# Save: Prefix + Ctrl+s
# Restore: Prefix + Ctrl+r

# tmux-continuum - automatic save/restore
set -g @plugin 'tmux-plugins/tmux-continuum'
set -g @continuum-restore 'on'

# tmux-yank - copy to system clipboard
set -g @plugin 'tmux-plugins/tmux-yank'

# tmux-pain-control - better pane management
set -g @plugin 'tmux-plugins/tmux-pain-control'

# tmux-sidebar - file tree sidebar
set -g @plugin 'tmux-plugins/tmux-sidebar'
# Toggle: Prefix + Tab

Command Line Interface

Session Management

bash
# Create session with command
tmux new-session -d -s mysession 'htop'

# Send commands to session
tmux send-keys -t mysession 'ls -la' C-m

# Capture pane content
tmux capture-pane -t mysession -p

# List windows in session
tmux list-windows -t mysession

# List panes in window
tmux list-panes -t mysession:1

Scripting Examples

bash
# Create development environment
create_dev_env() {
    local session="dev-$(date +%s)"
    tmux new-session -d -s "$session"
    tmux send-keys -t "$session" 'cd ~/project' C-m
    tmux split-window -t "$session" -h
    tmux send-keys -t "$session" 'cd ~/project && npm start' C-m
    tmux select-pane -t "$session":0
    tmux attach-session -t "$session"
}

# Kill all sessions except current
kill_other_sessions() {
    local current=$(tmux display-message -p '#S')
    tmux list-sessions -F '#{session_name}' | \
    grep -v "^$current$" | \
    xargs -I {} tmux kill-session -t {}
}

Tips and Tricks

Productivity Tips

bash
# Quick session switching
bind -n M-1 switch-client -t session1
bind -n M-2 switch-client -t session2

# Broadcast to all panes
bind a setw synchronize-panes

# Quick layouts
Prefix + Space  # Cycle through layouts
Prefix + Alt+1  # Even horizontal
Prefix + Alt+2  # Even vertical
Prefix + Alt+3  # Main horizontal
Prefix + Alt+4  # Main vertical
Prefix + Alt+5  # Tiled

# Zoom pane
Prefix + z      # Toggle pane zoom

# Break pane to new window
Prefix + !      # Break pane out

# Join pane from another window
bind j command-prompt -p "join pane from:" "join-pane -s '%%'"

Useful Aliases

bash
# Add to ~/.bashrc or ~/.zshrc
alias ta='tmux attach'
alias tls='tmux list-sessions'
alias tn='tmux new-session'
alias tk='tmux kill-session'

# Function to create or attach to session
tm() {
    [[ -n "$TMUX" ]] && change="switch-client" || change="attach-session"
    if [ $1 ]; then
        tmux $change -t "$1" 2>/dev/null || (tmux new-session -d -s $1 && tmux $change -t "$1"); return
    fi
    session=$(tmux list-sessions -F "#{session_name}" 2>/dev/null | fzf --exit-0) &&  tmux $change -t "$session" || echo "No sessions found."
}

Troubleshooting

Common Issues

bash
# Fix 256 color support
export TERM=screen-256color

# Fix clipboard issues
# Install xclip (Linux) or reattach-to-user-namespace (macOS)

# Reset tmux completely
tmux kill-server

# Check tmux version
tmux -V

# Validate configuration
tmux source-file ~/.tmux.conf

Performance

bash
# Reduce escape time
set -sg escape-time 0

# Increase history limit
set -g history-limit 50000

# Refresh status bar less frequently
set -g status-interval 5

Integration

With Vim/Neovim

bash
# vim-tmux-navigator plugin
# Seamless navigation between vim and tmux panes
bind -n C-h run "(tmux display-message -p '#{pane_current_command}' | grep -iq vim && tmux send-keys C-h) || tmux select-pane -L"
bind -n C-j run "(tmux display-message -p '#{pane_current_command}' | grep -iq vim && tmux send-keys C-j) || tmux select-pane -D"
bind -n C-k run "(tmux display-message -p '#{pane_current_command}' | grep -iq vim && tmux send-keys C-k) || tmux select-pane -U"
bind -n C-l run "(tmux display-message -p '#{pane_current_command}' | grep -iq vim && tmux send-keys C-l) || tmux select-pane -R"

With SSH

bash
# Auto-attach to tmux on SSH
# Add to ~/.bashrc on remote server
if [[ -z "$TMUX" ]] && [ "$SSH_CONNECTION" != "" ]; then
    tmux attach-session -t ssh_tmux || tmux new-session -s ssh_tmux
fi

Resources