Introduction
The tmux utility in Linux allows you to run and manage multiple processes within a single terminal session. You can create multiple sessions, split windows into panes, or detach from a session and reattach later without disrupting a running process.
Learn the essential tmux commands and download our handy cheat sheet for quick reference.
Prerequisites
- A Linux-based system.
- Access to a terminal window/command line.
- A user account with sudo or root privileges.
What is tmux?
tmux is a terminal multiplexer in Linux. It is a popular utility for system administrators as it allows them to open and manage several terminal sessions in one terminal window.
The tool works on all Linux distributions and is useful for managing long-running tasks over SSH. Users can detach from a session on one computer, switch to a second computer, reconnect to the server, and reattach to the same session without losing work.
Note: tmux comes pre-installed on most Linux distributions. If you don't have tmux installed, check out our guide on how to install tmux on Linux.
You can use tmux panes, windows, and sessions to organize tasks logically:
- Sessions. Use sessions to control an overall task from a unified interface. For example, you can keep all test-related activities within a single session.
- Windows. Launch specific activities or projects in separate windows within a session.
- Panes. Use panes to split a window into multiple views. For instance, you can work on a script in one pane while monitoring logs in another.
Note: tmux
command usage may differ from version to version. Your tmux version may not support all the commands listed below. For more information, see the official tmux changelog.
tmux Commands
Use tmux
commands in the terminal to launch sessions and manage windows and panes. However, managing multiple panes through direct CLI commands can be inefficient.
You can control tmux
using keyboard shortcuts. Keyboard shortcuts are a user-friendly way to navigate, manage content, and control the layout once a session is launched. CTRL+b is the default prefix key and must be followed by a specific hotkey to perform an action.
tmux Sessions
A tmux
session is the primary environment for managing windows and panes. A session groups related tasks or projects in a unified workspace. The following table lists essential commands for managing sessions:
Command | Description |
---|---|
tmux | Start a new session. |
tmux new -s [session_name] | Create a new session with a specific name. Replace session_name with the actual name of the session. |
tmux attach | Re-attach to the latest active session. |
tmux detach | Disconnect from a session, but keep it running. |
tmux attach -t [session_name] ortmux a -t [session_name] | Attach to session with a specific name. Replace session_name with the actual name of your session. |
tmux switch -t [session_name] | Switch to a different session. |
tmux rename-session -t [old_name] [new_name] | Rename an existing session. |
tmux list-sessions or tmux ls | List all active tmux sessions. |
tmux kill-session -t [session_name] | Terminate a specific session. |
tmux kill-session -a | Kill all sessions but the current one. |
tmux kill-session -a -t [session_name] | Terminate all sessions except the specified one. |
tmux kill-server | Terminate the tmux server, ending all sessions, windows, and panes. |
tmux Windows
Windows are used to separate individual tasks within a session and create a manageable window-based interface. The table lists commands for managing windows within tmux
sessions:
Command/Shortcut | Action |
---|---|
CTRL+b then c | Create a new window. |
CTRL+b then , | Rename the current window. |
CTRL+b then w | List all windows. |
CTRL+b then & | Kill the current window. |
CTRL+b then n | Switch to the next window. |
CTRL+b then p | Switch to the previous window. |
CTRL+b then l | Open the last window. |
CTRL+b then 0....9 | Switch to a specific numbered window. |
CTRL+b then d | Detach from the current session. |
tmux select-window -t [window_name] | Select a specific window by its index or name. |
tmux rename-window [new_name] | Rename the current window. |
tmux Panes
A pane is a basic element of a tmux window within a session. Managing many panes simultaneously can quickly clutter a window if not organized properly. The following shortcuts help you manage and organize panes efficiently:
Command/Shortcut | Action |
---|---|
CTRL+b then % | Split the current pane vertically. |
CTRL+b then " | Split the current pane horizontally. |
CRTL+b then x | Close the current pane. |
CTRL+b then o | Switch between panes. |
CTRL+b then z | Toggle pane zoom (make pane full screen). |
CTRL+b then ; | Toggle between the last two active panes. |
CTRL+b then { | Move the current pane left. |
CTRL+b then } | Move the current pane right. |
CTRL+b then SPACE | Toggle through different pane layouts. |
CTRL+b then ! | Convert the current pane into a window. |
exit orCTRL+d | Close the selected pane. |
CTRL+b then q | Display pane number. |
CTRL+b then : | Enter command mode. The bottom bar will change from green to yellow. Now you can type a command to resize the pane:resize-pane -D - Move the boundary line for the pane downward.resize-pane -U - Move the boundary line for the pane upward.resize-pane -R - Move the boundary line for the pane right.resize-pane -L - Push the boundary line for the pane left.resize-pane -U 10 - Move the boundary line up 10 cells.resize-pane –t 2 --R 5 - Move the boundary line of cell number 2 five cells to the right.resize-pane -Z - Maximize the pane to full screen. Use the same command to set it back to normal. |
CTRL+b+[up_arrow ] | Increase pane height. |
CTRL+b+[down_arrow ] | Decrease pane height. |
CTRL+b+[left_arrow ] | Increase pane width. |
CTRL+b+[right_arrow ] | Decrease pane width. |
Note: Resizing only works along shared boundaries between panes. You can't resize a pane if it's already at the edge of the screen. Also, adjusting a shared boundary affects adjacent panes. For example, resizing the top boundary of one pane will change the size of the pane below it.
tmux Copy Mode
Copy Mode allows users to navigate through the output of a pane and select and copy text. This feature is indispensable in terminal sessions with a lot of output. You can manipulate pane content and transfer it between panes, windows, and sessions much more efficiently.
The following table lists the most useful Copy Mode commands:
Command/Shortcut | Action |
---|---|
CTRL+b then [ | Enter copy mode. |
q | Exit copy mode. |
SPACE | Start text selection in copy mode. |
ENTER | Copy the selected text. |
ESC | Clear the selected text and exit the copy mode. |
CTRL+b then ] | Paste the copied text. |
h | Move the cursor left. |
j | Move the cursor down. |
k | Move the cursor up. |
l | Move the cursor right. |
w | Move the cursor one word forward. |
b | Move the cursor one word backward. |
CTRL+u | Scroll up half a page. |
CTRL+d | Scroll down half a page. |
PgUp | Scroll up full page. |
PgDn | Scroll down full page. |
tmux Configuration
tmux is highly configurable and enables users to customize key bindings, window management, and visual aspects like status bars and panes. Тhe tmux configuration file, tmux.conf, allows you to make system-wide or user-specific changes.
To apply changes for all users, use a text editor, like Nano, to create or edit the file in the system directory:
sudo nano /etc/tmux.conf
To apply changes for a single user, create or edit the file in the user’s home directory:
sudo nano ~/.tmux.conf
Once the tmux.conf file is created, add custom commands and remap function keys. The following table contains common configuration examples and their descriptions:
Command | Description |
---|---|
set-option -g prefix C-a unbind C-b bind C-a send-prefix | Changes the default CTRL+b binding to CTRL+a to activate functions. |
unbind % bind h split-window –h | Remaps the horizontal split to CTRL+b+h . |
unbind '"' bind v split-window –v | Remaps the vertical split key to CTRL+b+v . |
set -g status-bg blue set -g status-fg black | Changes the color of the status bar (background to blue and foreground to black). You can also use a numerical code (0 – 255) to specify a color. |
setw -g monitor-activity on setw -g visual-activity on | Enables visual notifications for activity in windows. |
set -g base-index 1 | Starts window numbering at 1 instead of 0. |
set -g pane-base-index 1 | Starts pane numbering at 1 instead of 0. |
After making changes, save the file and exit the editor. If you have any active tmux sessions, the changes take effect until they are closed and restarted. Detaching and reattaching a session will not apply the new settings.
tmux Cheat Sheet
Download the tmux Commands Cheat Sheet in PDF format. Use this one-page reference sheet to access essential tmux commands whenever you need them.
Conclusion
The listed tmux commands and downloadable cheat sheet enable you to manage multiple sessions, panes, and windows more efficiently. Additionally, you can customize tmux to fit your workflow, giving you even greater control over the terminal environment.
Next, read this ultimate list of Linux commands all users should know.