Lei Mao bio photo

Lei Mao

Machine Learning, Artificial Intelligence, Computer Science.

Twitter Facebook LinkedIn GitHub   G. Scholar E-Mail RSS


Tmux is a very powerful terminal multiplexer which is extremely useful especially when you are using the remote server via SSH.

If we want to do multiple tasks simultaneously on the remote server, usually we have two ways to do it. We could SSH into the remote server and run everything in the background with an ‘&’ at the end of each terminal command. This is problematic if you want to monitor the process of each task. We could also open multiple windows, SSH into the remote server for each window, and run one task for each window. This is good for monitoring all the tasks, but the shortcoming is that you would have to type your SSH login information for each of the windows you opened. Sometimes it is also hard to find which window is doing which task if there are too many windows opened.

Tmux allows the user to create multiple sessions and each session could have multiple terminals. The user would be able to control multiple tasks in multiple windows via Tmux. No more multiple SSH logins anymore. However, Tmux is not very friendly to beginners because you would have to memorize a series of commands required for controlling Tmux. Although Tmux is much useful than a terminal emulator such as Gnome Terminator, many users would just like to use Tmux as a multi-window terminal emulator. However, Tmux does not memorize user settings such as pane layouts, so every time after reboot or restart the Tmux server, all of the user settings will be gone.

In this short tutorial, I am going through some of the basic concepts and commands for Tmux, and how to use a Tmux plugin, which is called Tmux Resurrect, to restore Tmux environment after reboot or Tmux server restart.

Tmux Usages


We install Tmux via apt.

$ sudo apt update
$ sudo apt install tmux


Tmux has sessions, windows, and panes. The hierarchy is that Tmux could have multiple sessions, a session could have multiple windows, a window could have multiple panes. On the server, users could follow certain conventions or rules to manage Tmux. For example, we could create a session for a specific project. In the project session, we could create multiple windows, and each window would be used for each specific task for the project. In the window, in order to finish the task more efficiently, we create multiple panes for purposes such as process monitoring and file management.

Dual Interface

Similar to Docker, Tmux has two layers of the interface, the local terminal outside Tmux, and the terminal inside Tmux. We could manage Tmux in both layers. While typing bash commands are equivalent in both interface, to manage the Tmux related stuff inside Tmux, we would need to use hotkeys so Tmux knows when to manage the Tmux related stuff. All the hotkeys are prefixed by Ctrl + b.

Tmux Console

In the Tmux terminal, we could call out Tmux console by Ctrl + b + : and run all the Tmux commands available for the local terminal without tmux prefix. For example, if there is a Tmux command for the local terminal like this.

$ tmux xxxxx

In the Tmux console in the Tmux terminal, we could do the equivalent thing by running the following command.


Note that : is the prefix of the Tmux console which we don’t type. : could be thought as $ tmux in the local terminal.

Create Sessions

In the local terminal, we create Tmux sessions by simply running one of the following three equivalent commands.

$ tmux
$ tmux new
$ tmux new-session

This will create a new session to the existing Tmux. If there is no previous Tmux session running, this will create the first Tmux session. If there are already Tmux sessions running, this will create an additional one.

In the Tmux terminal, to create Tmux sessions, we would need to first call out the Tmux console by hitting Ctrl + b + :. Just like Vim, we could then type commands in the Tmux console at the bottom of the Tmux session. We type the following command to create Tmux session.


Tmux requires at least one session to run. If the last session was closed, Tmux server will automatically close.

In the following tutorials, because the commands in the Tmux console in the Tmux terminal is a replicate of the commands in the local terminal, we are not going to elaborate on them.

Detach Sessions

To return to the local terminal from Tmux sessions, we usually do detach by hitting Ctrl + b + d. Everything would be still running in the backend.

In some scenarios, we could return to the local terminal by running the following command in the Tmux terminal.

$ exit

However, bear in mind that using this method the current session will exit and all the information in the current session will be lost.

Create Sessions With Names

Tmux, by default, uses natural integers as the name for sessions. This is sometimes inconvenient for project management. We could create sessions with names using the following commands in the local terminal.

$ tmux new -s [session-name]

View Sessions

To view Tmux sessions from the local terminal, run one of the following commands.

$ tmux ls
$ tmux list-sessions

We would see the Tmux session information like this.

$ tmux ls
deeplabv3: 1 windows (created Sun Sep 22 12:41:33 2019) [80x23]
resnet50: 1 windows (created Sun Sep 22 12:38:25 2019) [80x23]

In Tmux terminal, we check Tmux sessions by hitting Ctrl + b + s. The following information will show up.

(0)  + deeplabv3: 1 windows
(1)  + resnet50: 1 windows (attached)                                           

┌ resnet50 (sort: index) ──────────────────────────────────────────────────────┐
│ leimao@leimao-evolvx:~$                                                      │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
│                                    0:bash                                    │
│                                                                              │
│                                                                              │
│                                                                              │
│                                                                              │
[resnet50]0:[tmux]*                              "leimao-evolvx" 12:48 22-Sep-19

Hit Esc or q to exit the information.

Rename Sessions

To rename sessions, from the local terminal, we run the following command.

$ tmux rename-session [-t session-name] [new-session-name]

If [-t session-name] is not provided, the last session used will be renamed.

Alternatively, we may also hit Ctrl + b + $ to rename the current session in the Tmux terminal.

Kill Sessions

To kill all sessions, from the local terminal, we run the following command.

$ tmux kill-server

To kill specific sessions, from the local terminal, we run the following command.

$ tmux kill-session -t [session-name]

Attach Sessions

To attach to specific sessions, from the local terminal, we run the following command.

$ tmux attach -t [session-name]

Create/Close Windows

In Tmux session, we could have multiple windows. To create a window, in the Tmux terminal, we hit Ctrl + b + c. To kill the current window, in the Tmux terminal, we hit Ctrl + b + & (& is Shift + 7).

The windows in the sessions could have names. We rename the current window by hitting Ctrl + b + ,.

(0)  + deeplabv3: 2 windows (attached)                                          
(1)  + resnet50: 1 windows

┌ deeplabv3 (sort: index) ─────────────────────────────────────────────────────┐
│ o-evolvx:~$                          │o-evolvx:~$                            │
│                                      │                                       │
│                                      │                                       │
│                                      │                                       │
│                                      │                                       │
│             0:htop-monitor           │                1:main                 │
│                                      │                                       │
│                                      │                                       │
│                                      │                                       │
│                                      │                                       │
[deeplabv30:htop-monitor- 1:main*                "leimao-evolvx" 14:35 22-Sep-19

The window name could be identified in the session information.

Select Windows

Each window in the session, regardless of whether it has a name or not (actually its default name is always bash), would have a window id of natural integer 0, 1, etc. We select specific window by hitting Ctrl + b + window id.

Sometimes it is also convenient to use Ctrl + b + n to move to the next window, or Ctrl + b + p to move to the previous window.

Create/Close Panes

Each window in the session could have multiple panes, just like Gnome Terminator. To split the pane vertically, we hit Ctrl + b + %. To split the pane horizontally, we hit Ctrl + b + ". To close the current pane, we we hit Ctrl + b + x.

leimao@leimao-evolvx:~$                 │leimao@leimao-evolvx:~$ 
leimao@leimao-evolvx:~$                 │leimao@leimao-evolvx:~$ 
[deeplabv30:htop-monitor- 1:main*                "leimao-evolvx" 14:56 22-Sep-19

To toggle between panes in the window, we simply hit Ctrl + b + ///.

Use Mouse

To scroll using mouse, we have to enable mouse mode in Tmux.

:set -g mouse on

To disable mouse mode in Tmux

:set -g mouse off

To do copy and paste in Tmux or between Tmux and host, holding Shift while left clicking and selecting text followed by pressing Ctrl-Shift-C will copy to the X clipboard. Ctrl-Shift-V will paste from the X clipboard as well.

Alternatively, holding Shift while left clicking and selecting text followed by right clicking. To paste, holding Shift while clicking the middle button of the mouse.

Tmux Resurrect Usages


To install Tmux Resurrect, it is recommended to install Tmux Plugin Manager first. Please check the GitHub repo for installation instructions.

Then we add new plugin Tmux Resurrect to Tmux by adding set -g @plugin 'tmux-plugins/tmux-resurrect' to ~/.tmux.conf. An example of the ~/.tmux.conf would be

$ cat ~/.tmux.conf 
# List of plugins
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'
set -g @plugin 'tmux-plugins/tmux-resurrect'

# Other examples:
# set -g @plugin 'github_username/plugin_name'
# set -g @plugin 'git@github.com/user/plugin'
# set -g @plugin 'git@bitbucket.com/user/plugin'

# Initialize TMUX plugin manager (keep this line at the very bottom of tmux.conf)
run -b '~/.tmux/plugins/tpm/tpm'

Finally, we install the plugin by hitting Ctrl + b + I in the Tmux terminal. We would see the following information if the installation was successful.

TMUX environment reloaded.
Done, press ESCAPE to continue.

Save and Restore Tmux Environment

To save the Tmux environment, we hit Ctrl + b + Ctrl + s in the Tmux Terminal. If the save was successful, a message of Tmux environment saved! would pop up.

To restore the Tmux environment, we hit Ctrl + b + Ctrl + r in the Tmux Terminal. If the restore was successful, a message of Tmux restore complete! would pop up.

All the sessions, windows, and panels would be saved and restored with Tmux Resurrect. Some of the running commands, such as htop, would be restored as well.

Last Tricks

Prefix Key Binding

Sometimes hitting the hotkey prefix Ctrl + b could be tedious. We could set a single button hit for Ctrl + b. The right ⊞ Win key on my keyboard seems to be useless in Ubuntu, and we could bind the right ⊞ Win key to Ctrl + b.


Thank my friend Dong Meng for recommending Tmux Resurrect to me.

Final Remarks

We would see concept similarities between Tmux, Docker, and Vim. More comprehensive Tmux commands could be found on Tmux Cheat Sheet.