in

Tmux Tutorial, Hacker News

Tmux Tutorial, Hacker News


      

Introduction

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 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 asGnome 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 calledTmux Resurrect, to restore Tmux environment after reboot or Tmux server restart.

Tmux Usages

Installation

We install Tmux viaapt.

$sudoapt update$sudoapt install tmux

Concepts

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.

Dual Interface

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

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 withouttmuxprefix. 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$ tmuxin 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 hittingCtrl (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 hittingCtrlbd. 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]

View Sessions

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

$ (tmux) ls$tmux list-sessions

We would see the Tmux session information like this.

$TMUXLSdeeplabv3: 1 windows(created Sun Sep 22 12: 41: 33 2019)[80x23] resnet 50: 1 windows(created Sun Sep 22 (********************************************************************************: 38: 25 2019)[80x23]

In Tmux terminal, we check Tmux sessions by hittingCtrlbs. The following information will show up.

(0) deeplabv3: 1 windows (1) Resnet 50: 1 windows (attached) ┌ resnet 50 (sort: index) ─────────────────────── ───────────────────────────────┐ Ima leimao @ leimao-evolvx: ~ $ │ │ │ │ │ │ │ │ │ │ 0: bash │ │ │ │ │ │ │ │ │ └───────────────────────────────────────────────── ─────────────────────────────┘ [resnet50] 0: [tmux] * "leimao-evolvx" 12: 48 22 - Sep - 19

HitEscorqto exit the information.

Rename Sessions

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

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

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

Alternatively, we may also hitCtrlb$to rename the current session in the Tmux terminal.

Kill Sessions

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.

$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 hitCtrlb (c) . To kill the current window, in the Tmux terminal, we hitCtrlb&(&isShift (7) ).

The windows in the sessions could have names. We rename the current window by hittingCtrlb,.

( (0) ) deeplabv3: 2 windows (attached)(1) resnet 50: 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 name will be identified in the session information.

Select Windows

Each window in the session, regardless whether it has name or not (actually its default name is alwaysbash), would have a window id by natural integer0,1, etc. We select specific window by hittingCtrlb window id.

Sometimes it is also convenient to useCtrlbnto move to the next window, orCtrlbpto 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 hitCtrlb%. To split the pane horizontally, we hitCtrlb". To close the current pane, we we hitCtrlbx.

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 hitCtrlb///.

Tmux Resurrect Usages

Installation

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

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

$cat~/.tmux.conf# List of pluginsset-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 protected]/user/plugin'# set -g @plugin '[email protected]/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 hittingCtrlbIin 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 hitCtrlbCtrlsin the Tmux Terminal. If the save was successful, a message ofTmux environment saved!would pop up.

To restore the Tmux environment, we hitCtrlbCtrlrin the Tmux Terminal. If the restore was successful, a message ofTmux 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 ashtop, would be restored as well.

Last Tricks

Prefix Key Binding

Sometime hitting the hotkey prefixCtrlbcould be tedious. We could set a single button hit forCtrl (b) . The right⊞ Winkey on my keyboard seems to be useless in Ubuntu, and we could bind the right⊞ Win (key to) ******************** (Ctrl) b.

Acknowledgement

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 onTmux Cheat Sheet.

References

  • Tmux Cheat Sheet
  •   

  • Tmux Resurrect

Brave Browser
(Read More)
Payeer

What do you think?

Leave a Reply

Your email address will not be published. Required fields are marked *

GIPHY App Key not set. Please check settings

Why Open Source Misses the Point of Free Software (2007), Hacker News

Russian national confesses to biggest bank hack in US history, Ars Technica

Russian national confesses to biggest bank hack in US history, Ars Technica