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.
We install Tmux via
$ 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.
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
In the Tmux terminal, we could call out Tmux console by
: 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.
: is the prefix of the Tmux console which we don’t type.
: could be thought as
$ tmux in the local terminal.
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
:. 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.
To return to the local terminal from Tmux sessions, we usually do detach by hitting
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.
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]
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
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
q to exit the information.
To rename sessions, from the local terminal, we run the following command.
$ tmux rename-session [-t session-name] [new-session-name]
[-t session-name] is not provided, the last session used will be renamed.
Alternatively, we may also hit
$ to rename the current session in the Tmux terminal.
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]
To attach to specific sessions, from the local terminal, we run the following command.
$ tmux attach -t [session-name]
In Tmux session, we could have multiple windows. To create a window, in the Tmux terminal, we hit
c. To kill the current window, in the Tmux terminal, we hit
The windows in the sessions could have names. We rename the current window by hitting
(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.
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
1, etc. We select specific window by hitting
b + window id.
Sometimes it is also convenient to use
n to move to the next window, or
p to move to the previous window.
Each window in the session could have multiple panes, just like Gnome Terminator. To split the pane vertically, we hit
%. To split the pane horizontally, we hit
". To close the current pane, we we hit
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
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 'email@example.com/user/plugin' # set -g @plugin 'firstname.lastname@example.org/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
I in the Tmux terminal. We would see the following information if the installation was successful.
[0/0] TMUX environment reloaded. Done, press ESCAPE to continue.
Save and Restore Tmux Environment
To save the Tmux environment, we hit
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
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.
Prefix Key Binding
Sometimes hitting the hotkey prefix
b could be tedious. We could set a single button hit for
b. The right
⊞ Win key on my keyboard seems to be useless in Ubuntu, and we could bind the right
⊞ Win key to
Thank my friend Dong Meng for recommending Tmux Resurrect to me.
We would see concept similarities between Tmux, Docker, and Vim. More comprehensive Tmux commands could be found on Tmux Cheat Sheet.