← All cheat sheets

TMUX

Plain-text reference · 7 KB. Read it, search it (Ctrl-F) or print it.

tmux is a terminal multiplexer - run multiple terminals
in a single window and detach/reattach sessions.

STARTING TMUX#

tmux                            # Start new session
tmux new                        # Start new session
tmux new -s name                # New session with name
tmux new -s name -d             # New detached session

SESSIONS#

tmux ls                         # List sessions
tmux list-sessions              # List sessions (long form)
tmux attach                     # Attach to last session
tmux attach -t name             # Attach to named session
tmux a -t name                  # Short form
tmux kill-session -t name       # Kill session
tmux kill-server                # Kill all sessions
tmux rename-session -t old new  # Rename session

PREFIX KEY#

Default prefix: Ctrl+b (shown as C-b or ^b)
All commands below start with prefix unless noted.

PREFIX + KEY REFERENCE#

?       List all key bindings
:       Enter command mode
d       Detach from session
$       Rename session
s       List/switch sessions
(       Previous session
)       Next session

WINDOWS (TABS)#

c       Create new window
,       Rename current window
&       Kill current window
w       List windows
n       Next window
p       Previous window
0-9     Switch to window number
l       Last active window
'       Prompt for window index
.       Move window

PANES (SPLITS)#

%       Split horizontally (left/right)
"       Split vertically (top/bottom)
o       Switch to next pane
;       Toggle last active pane
x       Kill current pane
z       Toggle pane zoom (fullscreen)
{       Move pane left
}       Move pane right
!       Convert pane to window
q       Show pane numbers (then number to switch)
Space   Toggle pane layouts

PANE NAVIGATION#

Arrow   Move to pane in direction
o       Cycle through panes
q N     Go to pane number N

PANE RESIZING#

C-Arrow         Resize by 1 cell
M-Arrow         Resize by 5 cells (Alt+Arrow)
:resize-pane -D 10    Resize down 10 cells
:resize-pane -U 10    Resize up
:resize-pane -L 10    Resize left
:resize-pane -R 10    Resize right

COPY MODE#

[           Enter copy mode
]           Paste buffer
q           Exit copy mode
Space       Start selection (in copy mode)
Enter       Copy selection (in copy mode)
/           Search forward
?           Search backward
n           Next search match
N           Previous match

VI COPY MODE (if set)#

v           Start selection
y           Copy selection
Escape      Cancel

COMMAND MODE#

Prefix + : to enter command mode

:new-window
:new-window -n name
:kill-window
:kill-pane
:split-window
:split-window -h
:select-pane -t 0
:resize-pane -D 10
:swap-pane -D
:swap-pane -U
:setw synchronize-panes    # Type in all panes

CONFIGURATION (~/.tmux.conf)#

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

# Enable mouse
set -g mouse on

# Start windows at 1
set -g base-index 1
setw -g pane-base-index 1

# Vi mode
setw -g mode-keys vi

# Faster escape
set -sg escape-time 0

# Increase history
set -g history-limit 50000

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

# Easy reload
bind r source-file ~/.tmux.conf \; display "Reloaded!"

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

# Resize panes
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-style 'bg=#333333 fg=#ffffff'
set -g status-left '#[fg=green]#S '
set -g status-right '#[fg=yellow]%H:%M'

# Colors
set -g default-terminal "screen-256color"

RELOAD CONFIG#

# Inside tmux
:source-file ~/.tmux.conf

# Or bind to key (in config)
bind r source-file ~/.tmux.conf \; display "Reloaded!"

SCRIPTING#

# Create session with windows and panes
tmux new-session -d -s dev
tmux rename-window -t dev:0 'editor'
tmux send-keys -t dev:0 'vim' C-m
tmux new-window -t dev -n 'server'
tmux send-keys -t dev:1 'npm start' C-m
tmux split-window -t dev:1 -h
tmux send-keys -t dev:1.1 'npm test' C-m
tmux attach -t dev

# Run command in new window
tmux new-window -n logs 'tail -f /var/log/syslog'

# Send keys to pane
tmux send-keys -t session:window.pane 'command' Enter

SESSION MANAGEMENT#

# Save session (requires tmux-resurrect plugin)
Prefix + Ctrl+s

# Restore session
Prefix + Ctrl+r

# Or use tmuxinator/tmuxp for session configs

SYNCHRONIZE PANES#

# Type same input in all panes
:setw synchronize-panes on
:setw synchronize-panes off

# Toggle with keybind (add to config)
bind S setw synchronize-panes

LAYOUTS#

Prefix + Space    Cycle layouts
:select-layout even-horizontal
:select-layout even-vertical
:select-layout main-horizontal
:select-layout main-vertical
:select-layout tiled

BUFFER/CLIPBOARD#

:show-buffer              Show buffer content
:save-buffer file.txt     Save buffer to file
:load-buffer file.txt     Load file to buffer
:list-buffers             List all buffers
:choose-buffer            Interactive buffer select
:delete-buffer            Delete buffer

# Copy to system clipboard (if configured)
# Add to .tmux.conf:
bind -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "xclip -selection clipboard"

USEFUL COMMANDS#

# List all commands
tmux list-commands

# Show options
tmux show-options -g
tmux show-options -w

# Show keybindings
tmux list-keys

# Display message
tmux display-message "Hello"

# Run shell command
tmux run-shell "echo hello"

PLUGINS (TPM)#

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

# In ~/.tmux.conf:
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'
set -g @plugin 'tmux-plugins/tmux-resurrect'

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

# Install plugins: Prefix + I
# Update plugins: Prefix + U

TROUBLESHOOTING#

# Check tmux version
tmux -V

# Start with no config
tmux -f /dev/null

# Verbose mode
tmux -v

# Kill stuck session
tmux kill-session -t name

QUICK REFERENCE#

tmux                New session
tmux new -s name    Named session
tmux ls             List sessions
tmux a -t name      Attach to session

C-b d       Detach
C-b c       New window
C-b n       Next window
C-b p       Previous window
C-b %       Split horizontal
C-b "       Split vertical
C-b o       Next pane
C-b x       Kill pane
C-b z       Zoom pane
C-b [       Copy mode
C-b ?       Help

Defensive reference on CyberRamen. Offensive / red-team sheets live on OffensiveRamen.com.