πŸ–₯️ tmux Cheat Sheet

πŸ–₯️ tmux Cheat Sheet #

πŸ“‹ What is tmux? #

tmux (Terminal Multiplexer) is a powerful command-line tool that allows you to:

  • Create multiple terminal sessions within a single window
  • Detach and reattach to sessions (survive SSH disconnections)
  • Split windows into panes for multitasking
  • Share sessions between multiple users
  • Keep processes running in the background

πŸš€ Installation #

Ubuntu/Debian #

sudo apt update
sudo apt install tmux

CentOS/RHEL/Fedora #

# CentOS/RHEL 7
sudo yum install tmux

# CentOS/RHEL 8+ / Fedora
sudo dnf install tmux

macOS #

brew install tmux

Verify Installation #

tmux -V

πŸ—οΈ Core Concepts #

Hierarchy #

  • Session: Collection of windows (like a workspace)
  • Window: Collection of panes (like browser tabs)
  • Pane: Individual terminal instance (split screen)

Prefix Key #

Default prefix: Ctrl-b (press before any tmux command) All shortcuts below assume you press Ctrl-b first

🎯 Essential Commands #

Session Management #

# Start new session
tmux
tmux new
tmux new-session

# Start named session
tmux new-session -s mysession

# List sessions
tmux list-sessions
tmux ls

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

# Attach to last session
tmux attach

# Switch between sessions
tmux switch -t mysession

# Kill session
tmux kill-session -t mysession

# Kill all sessions
tmux kill-server

Key Bindings (Prefix + Key) #

Session Control #

  • d - Detach from session
  • s - List and switch sessions
  • $ - Rename current session
  • ( - Switch to previous session
  • ) - Switch to next session

Window Management #

  • c - Create new window
  • n - Next window
  • p - Previous window
  • 0-9 - Switch to window by number
  • w - List windows
  • , - Rename current window
  • & - Kill current window
  • f - Find window by name

Pane Management #

  • % - Split horizontally (left/right)
  • " - Split vertically (top/bottom)
  • arrow keys - Navigate between panes
  • o - Cycle through panes
  • q - Show pane numbers
  • x - Kill current pane
  • z - Toggle pane zoom (fullscreen)
  • ! - Break pane into new window
  • { - Move pane left
  • } - Move pane right
  • Ctrl+arrow - Resize pane

Copy Mode & Scrollback #

  • [ - Enter copy mode (scroll/search)
  • ] - Paste from tmux buffer
  • q or Esc - Exit copy mode

Copy Mode Navigation (vi-style) #

  • k/↑ - Up one line
  • j/↓ - Down one line
  • h/← - Left one character
  • l/β†’ - Right one character
  • Ctrl-u - Half page up
  • Ctrl-d - Half page down
  • Ctrl-b - Full page up
  • Ctrl-f - Full page down
  • g - Go to top
  • G - Go to bottom
  • / - Search forward
  • ? - Search backward
  • Space - Start selection
  • Enter - Copy selection

System #

  • ? - List all key bindings
  • : - Command prompt
  • r - Reload config file
  • t - Show clock

βš™οΈ Configuration (.tmux.conf) #

Create ~/.tmux.conf for persistent settings:

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

# Enable mouse support
set -g mouse on

# Faster command sequences
set -s escape-time 0

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

# Better prefix key (optional)
unbind C-b
set -g prefix C-a
bind C-a send-prefix

# Vi-style copy mode
setw -g mode-keys vi
bind-key -T copy-mode-vi v send-keys -X begin-selection
bind-key -T copy-mode-vi y send-keys -X copy-selection-and-cancel

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

# 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

# Better splitting
bind | split-window -h -c "#{pane_current_path}"
bind - split-window -v -c "#{pane_current_path}"

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

# Window status
setw -g window-status-current-style 'fg=black bg=white bold'
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'

πŸ”Œ Plugin Manager (TPM) #

Installation #

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

Add to .tmux.conf #

# List of 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-continuum'

# Initialize TMUX plugin manager (keep at bottom)
run '~/.tmux/plugins/tpm/tpm'

Plugin Management #

  • Prefix + I - Install plugins
  • Prefix + U - Update plugins
  • Prefix + Alt + u - Uninstall plugins
  • tmux-sensible: Sensible defaults
  • tmux-resurrect: Save/restore sessions
  • tmux-continuum: Automatic session saving
  • tmux-yank: Copy to system clipboard
  • tmux-pain-control: Better pane management

🎨 Advanced Features #

Session Scripting #

#!/bin/bash
# Create development session
tmux new-session -d -s dev
tmux split-window -h
tmux select-pane -t 0
tmux send-keys 'vim' C-m
tmux select-pane -t 1
tmux send-keys 'npm run dev' C-m
tmux attach-session -t dev

Custom Key Bindings #

# Quick session switching
bind-key -r f run-shell "tmux neww ~/.local/bin/tmux-sessionizer"

# Send commands to all panes
bind-key a set-window-option synchronize-panes

# Quick layouts
bind-key M-1 select-layout even-horizontal
bind-key M-2 select-layout even-vertical
bind-key M-3 select-layout main-horizontal
bind-key M-4 select-layout main-vertical
bind-key M-5 select-layout tiled

Copy to System Clipboard #

# macOS
bind -T copy-mode-vi y send-keys -X copy-pipe-and-cancel 'pbcopy'

# Linux
bind -T copy-mode-vi y send-keys -X copy-pipe-and-cancel 'xclip -in -selection clipboard'

πŸ› οΈ Troubleshooting #

Common Issues #

  • Colors not working: Add alias tmux='tmux -2' to shell config
  • Mouse not working: Ensure set -g mouse on in config
  • Config not loading: Check file location and syntax
  • Prefix not working: Verify no conflicts with shell shortcuts

Debugging #

# Check current settings
tmux show-options -g
tmux show-window-options -g

# List key bindings
tmux list-keys

# Check tmux info
tmux info

πŸ’‘ Pro Tips #

Workflow Optimization #

  1. Use named sessions for different projects
  2. Set up project templates with predefined layouts
  3. Use mouse mode for quick pane switching
  4. Customize status bar with useful information
  5. Learn copy mode for efficient text selection

Performance #

  • Increase history limit for long-running processes
  • Use set -s escape-time 0 for faster key response
  • Disable unused features to reduce memory usage

Integration #

  • SSH: Sessions survive connection drops
  • Git: Show branch in status bar
  • Docker: Run containers in separate panes
  • Monitoring: Use panes for logs, metrics, etc.

πŸ”— Useful Commands #

Information #

tmux info                    # System information
tmux list-commands          # All available commands
tmux show-messages          # Recent messages
tmux display-message '#S'   # Show session name

Advanced Session Management #

# Create session with specific directory
tmux new-session -s project -c ~/projects/myapp

# Create detached session
tmux new-session -d -s background

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

# Capture pane content
tmux capture-pane -t mysession -p > output.txt

Layouts #

# Predefined layouts
tmux select-layout even-horizontal
tmux select-layout even-vertical
tmux select-layout main-horizontal
tmux select-layout main-vertical
tmux select-layout tiled

# Custom layout
tmux select-layout "main-horizontal,162x48,0,0{81x48,0,0,0,80x48,82,0,1}"

πŸ“š Resources #


Master tmux and transform your terminal workflow! πŸš€