Stories
Slash Boxes
Comments

SoylentNews is people

Submission Preview

Link to Story

Tmux Tutorial

Accepted submission by Arthur T Knackerbracket at 2019-09-24 07:52:52
Code

Title: Tmux Tutorial

--- --- --- --- --- --- --- Entire Story Below --- --- --- --- --- --- ---

Arthur T Knackerbracket has found the following story [github.io]:

If we want to do multiple tasks simultaneously on the remote server, usually we have to 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 [github.io], 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 [github.com], to restore Tmux environment after reboot or Tmux server restart.

We install Tmux via apt.

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 some 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 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 know when to manage the Tmux related stuff. All the hotkeys are prefixed by Ctrl + b.

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.

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.

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

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.

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.

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

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.

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

We would see the Tmux session information like this.

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

Hit Esc or q to exit the information.

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

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.

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

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

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

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 + ,.

The name will be identified in the session information.

Each window in the session, regardless whether it has name or not (actually its default name is always bash), would have a window id by 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.

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.

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

To install Tmux Resurrect, it is recommended to install Tmux Plugin Manager [github.com] 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

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.

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.

Sometime 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.

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


Original Submission