🌈
<span>tmux</span>(Terminal Multiplexer) is a terminal multiplexer that allows you to manage multiple terminal sessions within a single terminal window and reconnect to those sessions after disconnection.
1. Installing tmux
Most Linux distributions come with <span>tmux</span> pre-installed. If not, you can install it using the following commands:
-
Debian/Ubuntu:
sudo apt install tmux -
CentOS/RHEL:
sudo yum install tmux -
Fedora:
sudo dnf install tmux
2. Basic Usage
1. Start a new session
tmux
2. Start a named session (recommended)
tmux new -s session_name
For example:
tmux new -s work
3. List all sessions
tmux ls
Output example:
0: 1 windows (created Tue Jun 10 10:00:00 2025)
work: 2 windows (created Tue Jun 10 10:05:00 2025)
4. Attach to a session
tmux attach -t session_name
For example:
tmux attach -t work
5. Detach from a session
Press the key combination in tmux:
Ctrl + b then d
6. End a session
Type in the tmux session:
exit
Or use the command to force end:
tmux kill-session -t session_name
3. Common Shortcuts (Prefix: <span>Ctrl + b</span>)
🌈
All shortcut operations take effect after entering a tmux session.
| Shortcut | Function |
|---|---|
<span>Ctrl + b c</span> |
Create a new window |
<span>Ctrl + b n</span> |
Switch to the next window |
<span>Ctrl + b p</span> |
Switch to the previous window |
<span>Ctrl + b w</span> |
Show window list and switch |
<span>Ctrl + b l</span> |
Switch to the last used window |
<span>Ctrl + b ,</span> |
Rename the current window |
<span>Ctrl + b %</span> |
Vertical split pane |
<span>Ctrl + b "</span> (double quote) |
Horizontal split pane |
<span>Ctrl + b o</span> |
Switch between panes |
<span>Ctrl + b x</span> |
Close the current pane |
<span>Ctrl + b [</span> |
Enter copy mode (scroll, select text) |
<span>Ctrl + b :</span> |
Enter tmux command (e.g., <span>kill-session</span>) |
4. Advanced Features
1. Shared sessions
tmux attach -t session_name
Multiple users can connect to the same session simultaneously, suitable for collaborative debugging.
2. Run tasks in the background and view logs
tmux new -d -s mytask 'ping google.com'
Then attach to view:
tmux attach -t mytask
3. Split screen operations
-
Vertical split:
Ctrl + b % -
Horizontal split:
Ctrl + b " -
Switch panes:
Ctrl + b o -
Resize panes (enter resize mode):
Ctrl + b Alt + arrow keys
5. Configuration File (~/.tmux.conf)
You can customize <span>tmux</span> behavior by editing <span>~/.tmux.conf</span>. Here are some common configuration examples:
# Set prefix to Ctrl + a (instead of default Ctrl + b)
set-option -g prefix C-a
unbind C-b
bind C-a send-prefix
# Enable mouse support (requires tmux 2.1+)
set -g mouse on
# Set status bar colors
set -g status-bg black
set -g status-fg white
# Show window list
set -g status-left '#(whoami)@#H'
# Set window auto-naming
set-option -g allow-rename off
Load the configuration file:
tmux source-file ~/.tmux.conf
6. Summary of Common Commands
| Operation | Command |
|---|---|
| List sessions | <span>tmux ls</span> |
| Create session | <span>tmux new -s session_name</span> |
| Attach to session | <span>tmux attach -t session_name</span> |
| Force attach to session | <span>tmux attach -t session_name -d</span> |
| Kill session | <span>tmux kill-session -t session_name</span> |
| Rename session | <span>tmux rename-session -t old_name new_name</span> |
| Send command to background session | <span>tmux send-keys -t session_name 'your_command' Enter</span> |
7. Frequently Asked Questions
Q: How to exit copy mode?
A: Press the <span>q</span> key to exit.
Q: Encountering <span>no current session</span> error?
A: This means you are not executing commands within a tmux session, please enter a session first.
Q: How to set window title?
A: Add the following to <span>.tmux.conf</span>:
set-option -g set-titles on
set-option -g set-titles-string "#T"
8. Example Workflows
Example 1: Start a background task and view output
tmux new -d -s task 'ping baidu.com'
tmux attach -t task
Example 2: Create multiple windows and switch
tmux new -s demo
Ctrl + b c # Create second window
Ctrl + b w # View window list
Ctrl + b n/p # Switch windows
✅ Tips
- 💡 Using named sessions makes it easier to manage multiple tasks.
- 💡 Mouse support (
<span>mouse on</span>) allows easy clicking to switch panes. - 💡 Use
<span>Ctrl + b [</span>to enter copy mode, press<span>Enter</span>to start selection, then press<span>Enter</span>again to copy. - 💡 Using a plugin system (like tpm) can extend functionality (like CPU monitoring, auto-completion, etc.).