What is tmux?
tmux, short for 'Terminal Multiplexer,' allows you to use a single terminal screen divided into multiple independent sessions, windows, and panes. It's an essential tool for keeping tasks running even if your SSH connection drops, or for monitoring multiple tasks simultaneously.
3 Core Concepts
tmux has a hierarchical structure: Session > Window > Pane.
- Session: The largest unit. A workspace containing multiple windows (e.g., 'project-A'). Even if you detach from a session, the tasks continue to run in the background.
- Window: Similar to a tab within a session. A single session can have multiple windows.
- Pane: A smaller terminal screen obtained by splitting a window into multiple parts (e.g., top for code editing, bottom for log monitoring).
Prefix Key
The key combination you press first to execute commands within `tmux`. The default is Ctrl+b. (e.g., to create a new window, press Ctrl+b, release, then press c.) In this guide, this key is referred to as [Prefix].
Main Commands (Shell)
Commands entered directly into the terminal (Shell) to manage tmux sessions.
1. Session Management
2. Help/Version
Generated command:
Try combining the commands.
Description:
`tmux` Executes the command.
Combine the above options to virtually execute commands with AI.
Main Keybindings (Inside tmux)
Keybindings used when attached to a `tmux` session. First, press [Prefix] (default Ctrl+b), then press the subsequent key.
Session and Window Management
- [Prefix] + d: Detach from session. Tasks continue to run in the background.
- [Prefix] + c: Create a new window
- [Prefix] + w: View window list
- [Prefix] + n: Move to the next window
- [Prefix] + p: Move to the previous window
- [Prefix] + [0-9]: Jump directly to the window of that number
- [Prefix] + ,: Rename current window
- [Prefix] + &: Close current window (requires confirmation)
Pane Management
- [Prefix] + %: Split current pane vertically (left/right)
- [Prefix] + ": Split current pane horizontally (top/bottom)
- [Prefix] + [arrow keys]: Move focus to the pane in the direction of the arrow key
- [Prefix] + o: Cycle through to the next pane
- [Prefix] + z: Toggle current pane full screen (Zoom)
- [Prefix] + x: Close current pane (requires confirmation)
Usage Scenario Examples (Shell)
A common flow for starting and managing tmux from the terminal.
Create and Attach to 'my-project' Session
tmux new -s my-project
Detach from Session
Inside a session, press [Prefix] (Ctrl+b) then the 'd' key to detach from the session. The tasks will continue to run.
Check List of Running Sessions
tmux ls
Reattach to 'my-project' Session
tmux attach -t my-project
Forcefully Terminate 'my-project' Session
tmux kill-session -t my-project
Installation
tmux can be easily installed via the package manager of most Linux distributions.
Debian/Ubuntu
sudo apt update && sudo apt install -y tmux
RHEL/CentOS/Fedora
sudo dnf install -y tmux
Arch Linux
sudo pacman -S --needed tmux
openSUSE
sudo zypper install -y tmux
Tips & Cautions
Recommended Settings/Habits
- Immediately after SSH connection: If you create a session like `tmux new -s work` immediately after connecting to a remote server, you can safely resume your work even if the connection is lost.
- Mouse Mode: Adding `set -g mouse on` to ~/.tmux.conf enables mouse-based pane resizing, window selection, and scrolling.
- Configuration File: Configure your own keybindings or themes in ~/.tmux.conf.
- Change Prefix: If Ctrl+b is inconvenient, you can change it to `set -g prefix C-a`.