Linux tmux Command
tmux (Terminal Multiplexer) is a terminal multiplexing tool that allows you to create multiple virtual terminal sessions in a single terminal window and keep these sessions running in the background. Compared with using the terminal directly, tmux provides more powerful session management capabilities.
Core Advantages:
- Session persistence: Even if the network disconnects, the session remains on the server
- Multi-window/pane management: Efficiently organize multiple work environments
- Session sharing: Multiple users can connect to the same session simultaneously
Install tmux
On most Linux distributions, tmux can be easily installed via the package manager:
Examples
sudo apt-get install tmux
# CentOS/RHEL
sudo yum install tmux
# macOS (using Homebrew)
brew install tmux
After installation, enter thetmuxcommand to start a new session.
Basic Concepts
Session
A tmux session is an independent runtime environment that can contain multiple windows. Even if the connection is disconnected, the session continues to run in the background.
Window
Each session can contain multiple windows, similar to tabs in a browser.
Pane
Each window can be split into multiple panes, allowing you to view and operate multiple terminals simultaneously.
Common Commands and Shortcuts
All tmux operations require pressing the prefix key first (default isCtrl+b), then entering the command key.
Session Management
| Command/Shortcut | Description |
|---|---|
tmux new -s <name> |
Create a new session named name |
Ctrl+b d |
Detach the current session (the session continues running in the background) |
tmux ls |
List all sessions |
tmux attach -t <name> |
Reconnect to the specified session |
Ctrl+b $ |
Rename the current session |
Ctrl+b s |
Switch sessions |
Window Management
| Command/Shortcut | Description |
|---|---|
Ctrl+b c |
Create a new window |
Ctrl+b & |
Close the current window |
Ctrl+b n |
Switch to the next window |
Ctrl+b p |
Switch to the previous window |
Ctrl+b <number> |
Switch to the window with the specified number |
Ctrl+b , |
Rename the current window |
Pane Management
| Command/Shortcut | Description |
|---|---|
Ctrl+b % |
Split the current pane vertically |
Ctrl+b " |
Split the current pane horizontally |
Ctrl+b <arrow> |
Move focus between panes |
Ctrl+b x |
Close the current pane |
Ctrl+b z |
Maximize/restore the current pane |
Ctrl+b Space |
Switch pane layout |
Configure tmux
tmux's configuration file is located at~/.tmux.conf. Here are some common configuration examples:
Examples
unbind C-b
set -g prefix C-a
bind C-a send-prefix
# Enable mouse support (can click to switch panes/windows)
set -g mouse on
# Set the status bar color
set -g status-bg black
set -g status-fg white
# Set the pane border color
set -g pane-border-style fg=green
set -g pane-active-border-style fg=red
# Set windows to start numbering from 1 (default is 0)
set -g base-index 1
setw -g pane-base-index 1
After modifying the configuration, pressCtrl+b :then entersource-file ~/.tmux.confto reload the configuration.
Practical Tips
1. Session Recovery
Even if the server restarts, tmux sessions can be recovered:
Examples
git clone https://github.com/tmux-plugins/tmux-resurrect ~/clone/path
# Add to .tmux.conf
set -g @plugin 'tmux-plugins/tmux-resurrect'
2. Copy Mode
Ctrl+b [Enter copy mode- Use the arrow keys to move the cursor
- Space to start selecting, Enter to copy
Ctrl+b ]Paste
3. Synchronized Input
Enter the same command in multiple panes simultaneously:
Examples
Ctrl+b :setw synchronize-panes on
# Turn off synchronized mode
Ctrl+b :setw synchronize-panes off
4. Quickly Create a Development Environment
Examples
tmux new -s dev -d
tmux send-keys -t dev:0 "vim" C-m
tmux split-window -h -t dev:0
tmux split-window -v -t dev:0.1
tmux attach -t dev
Common Problem Solutions
1. Cannot Use Mouse Scrolling
In.tmux.confAdd to:
set -g terminal-overrides 'xterm*:smcup@:rmcup@'
2. Colors Display Abnormally
Ensure the terminal supports 256 colors, add:
set -g default-terminal "screen-256color"
3. Shortcut Key Conflicts
If some shortcuts conflict with your other tools, you can rebind them in the configuration file.
Learning Path Suggestions
- First master basic session management (create, detach, reconnect)
- Practice basic operations of windows and panes
- Familiarize yourself with common shortcuts
- Customize the configuration according to your needs
- Explore advanced features and plugins
Through continuous practice, you will gradually discover how tmux can greatly improve your terminal work efficiency.
Other Extensions
Linux Command Encyclopedia