Introduction
Tmux is a terminal multiplexer, enabling multiple terminal sessions to run simultaneously. It creates several windows, known as "pseudo-terminals," and allows splitting these windows into multiple "panes." Each window operates independently, supporting multiple processes within a single SSH connection.
The key benefit of tmux is maintaining processes even if you disconnect from a session. You can later reconnect and resume where you left off, which is particularly useful when working over SSH. If the SSH connection drops, you'll detach from the tmux session, but all processes will continue running in the background.
1. Installing Tmux
The installation command for tmux varies by Linux distribution.
For Debian-based distributions (Ubuntu, Debian), run:
apt install tmux
- For RedHat-based distributions (CentOS, AlmaLinux), execute:
yum install tmux
2. Creating a Tmux Session
- To start a tmux session, use the command:
tmux
- This will initiate a new tmux session named "0". Each subsequent command creates a new session with an incremented number (0, 1, 2...).
3. Creating a Window
- To create a new window within tmux, use the keyboard shortcut CTRL + B followed by C:
CTRL+B C
4. Switching Between Windows
- Navigate between windows by pressing CTRL + B followed by the window number (0 for the first window, 1 for the second, etc.):
CTRL+B 0 (1, 2...)
5. Splitting Tmux Windows Vertically
- To split a window vertically, use:
CTRL+B %
- This creates two vertical panes, allowing independent processes in each.
6. Splitting Tmux Windows Horizontally
- For a horizontal split, press:
CTRL+B "
- You can combine vertical and horizontal splits to create multiple panes within a single window.
7. Navigating Between Panes
- Move between panes using CTRL + B followed by an arrow key (Left, Right, Up, Down):
CTRL+B <arrow key>
- For instance, to switch from the right pane to the left, press:
CTRL+B <-
exit
8. Detaching from a Tmux Session
- Detach from the current session with:
CTRL+B D
9. Creating a Named Session
- To create a session with a specific name, use:
tmux new -s [name]
10. Running Background Processes in Tmux
- To demonstrate tmux's background capabilities, copy two large files using rsync:
- Even if the rsync process is ongoing, you can detach from the tmux session and close the SSH connection without interrupting the process. Reconnect to the server and reattach to the session later with:
tmux attach-session -t [Session name]
- The background process will complete without interruption.
11. Listing Tmux Sessions
- List all tmux sessions with:
tmux ls
- The output includes the number of windows in each session.
12. Killing Tmux Sessions
- To terminate a specific session, use:
tmux kill-session -t [name]
- To kill all sessions, type:
tmux kill-server
13. Customizing Tmux Shortcuts
- Change the default CTRL + B prefix to a more convenient shortcut by creating a configuration file:
vi $HOME/.tmux.conf
- Set a new prefix, such as CTRL + Q, and unbind the old one:
set -g prefix C-q
unbind C-b
- Save the file and reload it:
tmux source-file $HOME/.tmux.conf
- Now, all tmux shortcuts will use CTRL + Q.
14. Using the Mouse to Resize Panes
- Enable mouse support by adding this line to your configuration file:
set -g mouse on
- Save the changes, and you can now resize panes with your mouse by dragging the lines between them.