Linux tmux command
tmux (Terminal Multiplexer) is a terminal multiplexer 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 a terminal directly, tmux provides more powerful session management features.
Core Advantages:
- Session persistence: even if the network is disconnected, 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 at the same time
Installing tmux
On most Linux distributions, tmux can be easily installed through a package manager:
Example
Ubuntu/Debian
sudo
apt-get install
tmux
CentOS/RHEL
sudo
yum install
tmux
macOS (using Homebrew)
brew
install
tmux
After installation, enter tmux command to start a new session.
Basic concepts
Session
A tmux session is an independent running environment that can contain multiple windows. Even if 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 at the same time.
Common commands and shortcuts
All tmux operations require first pressing the prefix key (by default Ctrl+b), then entering the command key.
Session management
| Command/shortcut | Description |
|---|---|
tmux new -s |
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 |
Reconnect to a 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 |
Switch to a window with a 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 |
Move focus between panes |
Ctrl+b x |
Close the current pane |
Ctrl+b z |
Maximize/restore the current pane |
Ctrl+b Space |
Switch pane layouts |
Configuring tmux
The tmux configuration file is located at ~/.tmux.conf. Here are some common configuration examples:
Example
Set the prefix key to Ctrl+a (easier to press than the default Ctrl+b)
unbind C-b
set
-g
prefix C-a
bind
C-a send-prefix
Enable mouse support (you 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
Number windows starting from 1 (the default is 0)
set
-g
base-index
1
setw
-g
pane-base-index
1
After modifying the configuration, you can press Ctrl+b : then enter source-file ~/.tmux.conf to reload the configuration.
Practical tips
1. Session recovery
Even if the server restarts, tmux sessions can be restored:
Example
Install plugins
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
- Press space to start selecting, Enter to copy
Ctrl+b ]Paste
3. Synchronized input
Enter the same command in multiple panes at the same time:
Example
Enter synchronization mode
Ctrl+b :setw synchronize-panes on
Turn off synchronization mode
Ctrl+b :setw synchronize-panes off
4. Quickly create a development environment
Example
Create a development session with 3 panes
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 scroll with the mouse
in .tmux.conf add the following:
set -g terminal-overrides 'xterm*:smcup@:rmcup@'
2. Colors are displayed incorrectly
Make sure the terminal supports 256 colors; add:
set -g default-terminal "screen-256color"
3. Shortcut conflicts
If some shortcuts conflict with your other tools, you can rebind them in the configuration file.
Suggested Learning Path
- First master basic session management (create, detach, reconnect)
- Practice basic window and pane operations
- Familiarize yourself with common shortcuts
- Customize the configuration as needed
- Explore advanced features and plugins
Through continuous practice, you will gradually discover how tmux can greatly improve your terminal work efficiency.
Linux Command Reference