tmux can manage multiple windows and split panes in a terminal, and can keep tasks running after an SSH disconnect.
Its default prefix key is Ctrl+b. Most shortcuts use the following sequence:
- Press
Ctrl+b; - Release both keys;
- Press the following action key.
For example, to create a new window, instead of pressing
Ctrl+b+cat the same time, pressCtrl+bfirst, release it and then pressc.
The most important shortcut keys to remember
|
|
Restore the session you just exited:
|
|
Install and confirm that tmux is available
Debian and Ubuntu:
|
|
Fedora:
|
|
Arch Linux:
|
|
macOS using Homebrew:
|
|
Check the version after installation:
|
|
The terminal returns information similar to tmux 3.x, indicating that the command has entered PATH in the current environment.
Create, exit, and resume sessions
Start an anonymous session directly:
|
|
It is more recommended to create a named session:
|
|
work can be replaced by the project name, server name or task name.
Press Ctrl+b when leaving temporarily, and press d after releasing. This operation is called detach, which only detaches the current terminal from the session. Compilation, downloads, log monitoring, and scripts within the session will still run.
If exit is executed in the pane, the current shell will be closed. After the last pane exits, the corresponding window and session may also end.
| operate | Command or shortcut key |
|---|---|
| Create a new named session | tmux new -s session-name |
| View session | tmux ls |
| Restore recent conversations | tmux attach |
| Restore specified session | tmux attach -t session-name |
| temporary exit | Ctrl+b, then press d |
| Rename current session | Ctrl+b, then press $ |
| Delete specified session | tmux kill-session -t session-name |
When there are multiple sessions, tmux attach may prompt that the target is unclear. Check the name first: |
|
|
Then resume the specified session:
|
|
Shorthand commands also work:
|
|
Window shortcut keys
Windows are similar to browser tabs. A session can have multiple windows, and each window can contain multiple panes.
| operate | shortcut key |
|---|---|
| New window | Ctrl+b, then press c |
| next window | Ctrl+b, then press n |
| Previous window | Ctrl+b, then press p |
| Recently used windows | Ctrl+b, then press l |
| Switch to windows 0–9 | Ctrl+b, then press the number keys |
| View window list | Ctrl+b, then press w |
| Rename window | Ctrl+b, then press , |
| Close current window | Ctrl+b, then press & |
tmux will ask for confirmation when closing the window. After typing y, all panes and programs in the window will end. Do not confuse & with d used by detach. |
|
When the window number exceeds 9, use Ctrl+b w to select from the list. You can also press Ctrl+b ', enter the window number and press Enter. |
Split pane and pane operation
Panes are independent terminal areas within the same window.
| operate | shortcut key |
|---|---|
| Split pane left and right | Ctrl+b, then press % |
| Split pane up and down | Ctrl+b, then press " |
| Switch pane | Ctrl+b, then press the arrow keys |
| Switch to next pane | Ctrl+b, then press o |
| Display pane number | Ctrl+b, then press q |
| Current pane full screen or restored | Ctrl+b, then press z |
| Close current pane | Ctrl+b, then press x |
| Rotate preset layouts | Ctrl+b, then press space |
After pressing Ctrl+b q, each pane will briefly display the number. Press the corresponding number while the number is still on the screen to jump directly. |
|
Ctrl+b z only changes the display range and does not delete other panes; execute it again to restore the original layout. |
Resize paness
The common operation is to press Ctrl+b first, then press and hold Ctrl and press the direction keys continuously. If the terminal intercepts the key combination, you can use the tmux command.
First press Ctrl+b :, then enter:
|
|
L, R, U, and D represent left, right, top, and bottom respectively, and the numbers represent the number of units to be adjusted.
Set the current pane width or height as a percentage:
|
|
Scroll through terminal history
The scroll wheel of a normal terminal sometimes cannot directly scroll the tmux history. First enter copy mode:
|
|
| operate | button |
|---|---|
| Move line by line | Arrow keys |
| Page up | PageUp |
| Page down | PageDown |
| jump to top | g, need to use vi mode |
| jump to bottom | G, need to use vi mode |
| search | /, need to use vi mode |
| Exit copy mode | q or Esc |
If g, G, and / behave differently, it’s usually because emacs-style keystrokes are being used. Vi mode can be enabled in ~/.tmux.conf: |
|
|
Copy and paste text
When using vi mode, the common process is as follows:
- Press
Ctrl+b [to enter copy mode; - Move to starting point;
- Press space to start selection;
- move to end point;
- Press Enter to copy to the tmux buffer;
- Press
Ctrl+b ]to paste. Different versions or custom configurations may usevto start selection andyto complete copying. PressCtrl+b ?to view the current real binding. View and save tmux buffers:
|
|
Enable mouse operation
When you want to use the mouse to select panes, adjust borders and scroll, add in ~/.tmux.conf:
|
|
Reload configuration:
|
|
When you turn on the mouse, the scroll wheel usually automatically enters copy mode. In some terminals, mouse selection will be taken over by tmux; when you need to use the terminal’s native selection, you can try holding down Shift and dragging.
A sufficient basic configuration
Edit ~/.tmux.conf:
|
|
After saving, execute:
|
|
You can also press Ctrl+b : and enter source-file ~/.tmux.conf. When there are syntax errors in the configuration, tmux will point out the line number; comment out the recently added configuration and try again without deleting the entire file.
Modify default prefix key
If Ctrl+b conflicts with the editor shortcut key, you can change it to Ctrl+a:
|
|
After loading, all Ctrl+b operations in this article must be replaced with Ctrl+a. The last line is used to send the prefix to the nested tmux.
Resume task after SSH disconnect
When running a long task remotely, you can follow the following process:
|
|
When you need to leave, press Ctrl+b d and then exit SSH. Sessions often survive unexpected network disconnections.
After reconnecting execute:
|
|
tmux can only save terminal sessions and cannot automatically revive programs that have exited with an error. Important tasks should still be logged:
|
|
Common errors and solutions
no server running
When tmux ls is executed, no server running on /tmp/tmux-... appears, indicating that the current user does not have a running tmux server. Just recreate the session:
|
|
sessions should be nested with care
This means that the current shell is already in tmux and tmux has been executed. First check:
|
|
When there is output, first press Ctrl+b c to create a new window, or use the following command to switch sessions:
|
|
The display size is wrong after attach
When terminals of different sizes are connected to the same session, the display area may be affected by the smaller client. Other clients can be disconnected during recovery:
|
|
View existing clients:
|
|
Shortcut keys are not responding
Check in order:
- Whether to press the prefix first and release;
- Whether the prefix has been modified in the configuration;
- Whether the terminal or remote desktop intercepts the key combination;
- Whether in nested tmux;
- Whether the current binding is overwritten by the plugin. Show all real bindings:
|
|
Query split-screen bindings from the shell:
|
|
A pane or session was closed accidentally
tmux does not have a general undo shutdown function. Before closing, make sure there are no unsaved editors, interactive tasks, or unwritten output to files. Use tee to save the log, which can reduce the loss after accidentally closing the pane.
Safe shutdown and cleanup
Just end a session:
|
|
Keep work and end other sessions:
|
|
End the entire tmux server for the current user:
|
|
kill-server will close all sessions managed by the server and the processes in them. Before execution, run tmux ls to confirm that there are no tasks that still need to be retained.
Precisely specify sessions, windows and panes
The tmux command often uses -t to specify the target. The complete target format is:
|
|
For example, work:1.2 represents window 1, pane 2 in the work session.
View current goals:
|
|
List all windows:
|
|
List all panes and their current commands:
|
|
This writing method is suitable for troubleshooting task locations between multiple sessions, and is also suitable for scripts to only operate specified panes to avoid accidentally closing other terminals.
The target can omit some fields. Window 2 in the current session can be written as :2, and pane 1 in the current window can be written as .1. It is recommended to use the complete target in automation scripts to reduce ambiguity caused by changes in the current focus.
Create windows and split panes from the shell
In addition to keyboard shortcuts, tmux commands can also be called directly. Create a new window to the work session:
|
|
Create left and right split panes in the window:
|
|
Create a top-bottom split pane:
|
|
Run the command directly when creating the pane:
|
|
The pane with the command will close after the command ends. If you want to see the final output, you can tell the shell to persist after the command:
|
|
In a remote production environment, do not write commands that require passwords directly into the Shell history. Use existing authentication mechanisms and limit the log pane to read only necessary files.
Send commands to a background session
send-keys can simulate keyboard input to the specified pane:
|
|
Send Ctrl+c to the running foreground program:
|
|
Before sending the command, confirm the program currently running on the pane:
|
|
If the output is an editor, database client, or other interactive program, sending text directly may cause data modification. When automating, prepare dedicated sessions and fixed window names for tasks, and do not rely on temporary numbers.
Create a background session and start the task:
|
|
Check if the session exists:
|
|
A return code of 0 indicates that the target session exists. It only shows that the tmux session is still there, not that the script was successful. You should also check the exit status, log, or output file.
Capture pane output for troubleshooting
You can also read the pane history without entering a session:
|
|
Get only the last 100 rows:
|
|
Save to file:
|
|
capture-pane reads the screen and history content saved by tmux. If the program output exceeds history-limit, earlier content cannot be restored.
Tasks that require long-term auditing should have the program write its own logs, rather than relying solely on tmux history. If the logs contain tokens, connection strings, or user data, appropriate file permissions and retention periods must also be set.
Check if the session still has clients
The following command can check which terminals are still connected to tmux:
|
|
If an exclusive session is required before the maintenance window, notify other users first, and then decide whether to use attach -d to disconnect their clients.
An empty client list does not mean that the task has ended, it only means that no terminal is currently displaying the session.
FAQ
Will detach stop the program?
Won’t. Ctrl+b d only disconnects clients. Whether the program continues to run also depends on whether the server is shut down, restarted, or the process is terminated by another mechanism.
Why did Ctrl+b % enter a percent sign?
Usually there is no trigger prefix, or the keys are pressed in a simultaneous combination. Press Ctrl+b first and release it all before entering %; most keyboard layouts require Shift+5.
Why is it difficult to press double quotes?
Trigger the prefix first, and then use the double quote combination corresponding to the current keyboard layout, usually Shift+'. You can also rebind a more convenient split-screen key in the configuration.
How to confirm current session, window and pane?
|
|
The output is session name, window number, and pane number, in order.
Quick recap
The structure of tmux is divided into three layers: the session saves the entire working environment, the window distinguishes tasks, and the pane is responsible for splitting the screen.
The daily process is to use tmux new -s name to create a session, organize tasks through windows and split panes, press Ctrl+b d to leave safely, and then use tmux attach -t name to resume.
When you forget a shortcut, press Ctrl+b ? to see the real bindings in your current configuration, which is more reliable than memorizing the full list.