正在加载,请稍候…

tmux: Terminal Multiplexer Mastery

Master tmux for managing multiple terminal sessions, windows, and panes. Learn essential shortcuts, configuration, and scripting for a productive terminal workflow.

tmux: Terminal Multiplexer Mastery

tmux lets you run multiple terminal sessions, keep processes alive after SSH disconnects, and create organized workspaces.

Installation and Start

# Install
brew install tmux        # macOS
apt install tmux         # Ubuntu

# Start new session
tmux new -s mysession

# Attach to existing session
tmux attach -t mysession

# List sessions
tmux ls

Key Shortcuts (prefix = Ctrl+b)

Sessions:
  prefix + d    Detach from session
  prefix + $    Rename session
  prefix + s    List/switch sessions

Windows (tabs):
  prefix + c    Create new window
  prefix + ,    Rename window
  prefix + n    Next window
  prefix + p    Previous window
  prefix + &    Kill window
  prefix + 0-9  Switch to window by number

Panes (splits):
  prefix + %    Split vertically
  prefix + "    Split horizontally
  prefix + Arrow Navigate between panes
  prefix + z    Zoom/unzoom pane
  prefix + x    Kill pane
  prefix + {    Move pane left
  prefix + }    Move pane right

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

# Better colors
set -g default-terminal "screen-256color"
set -ga terminal-overrides ",*256col*:Tc"

# Start windows at 1 (not 0)
set -g base-index 1
setw -g pane-base-index 1

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

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

# Status bar
set -g status-style 'bg=#333333 fg=#5eacd3'
set -g status-left '#[bold]#S '
set -g status-right '#(date +"%H:%M")'

Scripting with tmux

#!/bin/bash
# Create dev environment
tmux new-session -d -s dev -n editor

tmux send-keys -t dev:editor "nvim ." Enter

tmux new-window -t dev -n server
tmux send-keys -t dev:server "npm run dev" Enter

tmux new-window -t dev -n git
tmux send-keys -t dev:git "git status" Enter

tmux select-window -t dev:editor
tmux attach -t dev

Copy Mode (scrollback)

prefix + [       Enter copy mode
q                Exit copy mode
/                Search forward
?                Search backward
v                Begin selection (vi mode)
y                Copy selection
prefix + ]       Paste

TPM (Plugin Manager)

# 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-resurrect'  # Save/restore sessions
set -g @plugin 'tmux-plugins/tmux-continuum'  # Auto-save

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

tmux is essential for remote server work and local multi-project development.