⬆️ ⬇️

Terminal multiplexer overview: tmux and dvtm

Matrix Architect



The terminal is one of the most commonly used admin tools. Often during operation, you have to keep several tabs of the terminal open at the same time: one to manage the web server, another to manage the database, the third to copy files, etc. When there are a lot of open tabs, it becomes difficult to track all processes at the same time, and this significantly complicates the work. There are special programs with which you can solve the problem of multiple terminals. They are called terminal multiplexers.



Terminal multiplexers allow you to simultaneously work with several terminal sessions in one window. The principle of their operation is as follows: a terminal with multiple tabs is created on a remote machine; on the local machine, the terminal window is divided into several parts, and in each of them any applications and processes can be launched. You can disconnect from the remote machine (the current session will be saved), and then reconnect to it and continue working.



The most famous and common terminal multiplexer is, of course, the GNU Screen. With all its advantages, it differs in some inconvenience in work. The operation of splitting the screen into parts, for example, was not successfully implemented: when disconnected from the current session and then resumed, the order of the parts is not preserved.

')

Recently, other terminal multiplexers, more visual and convenient in operation, such as tmux and dvtm, to which this article is devoted, have become widespread.



Tmux



Tmux works under Linux and under MacOS; it is included in the official repositories of most modern Linux systems and can be installed using the standard package manager.



Run tmux with the following command:



 $ tmux


A status bar appears at the bottom of the screen:



tmux



All terminal multiplexers are controlled as follows: first, the so-called prefix key combination is pressed, and after it, the key that controls a particular function. In tmux, the default Ctrl + B is set as prefix. To split the terminal screen into two halves with a vertical line, you must first press Ctrl + B, and then% (i.e. Shift + 5):



tmux 1



Each of the resulting tabs can also be divided into two parts by a horizontal line using the key combination Ctrl + B + ”(first press Ctrl + B and then“).



You can also divide the window into two parts using the commands:



 $ tmux split-window -h


(divided into two parts horizontally)



 $ tmux split-window -v


(divided into two parts vertically)



To move from one part of the window to another, first press Ctrl + B, and then - the ←, ↑, → and ↓ keys.



To create a new window, press the key combination Ctrl + B + C. An unlimited number of windows can be involved in one session; each of them can be in turn divided into parts. Switching between windows is performed using the following combinations:







The current tmux session can be interrupted by pressing the key combination Ctrl + B + D. To resume an interrupted session, enter the following command:

  $ tmux attach 




An interrupted session will be restored; all data and processes will be saved.

This command can be entered in abbreviated form:



  $ tmux -a 




To display a list of sessions, run the following command:



  $ tmux ls 




The end of an open session is accomplished using the command:



  $ tmux kill-session -t [session name] 




To complete all open sessions at the same time, enter the command:



  $ tmux kill-server 




Customization Tips





The display of the status bar at the bottom of the screen can be turned off. To do this, enter the following command:



  $ tmux set -g status off 




And so that the disabled status bar is displayed again - the command:



  $ tmux set -g status on 




You can change the position of the status bar and make it so that it is not at the bottom, but at the top of the screen:



  $ set -g status-position top 




The color of the status bar is changed with the following command:



  tmux set -g status-bg blue 




Dvtm





Dvtm (dynamic virtual terminal terminal manager) is a terminal multiplexer, in functionality in many respects close to tmux.

It is also included in the official repositories of most modern Linux systems and installed using the standard package manager.

After starting dvtm, a status bar will be displayed at the top of the screen:



dvtm



The dvtm control is organized in the same way as tmux control. The prefix is ​​Ctrl + G. The concept of a window in dvtm is interpreted in a completely different way compared to tmux. To create a new window, use the key combination Ctrl + G + C. When you press this combination, the screen will be divided into two parts; while each of them is a separate window:



dvtm



Using the same key combination, you can create additional windows:



dvtm



The order of the windows can be changed. The following layout options are available:





You can switch from one window positioning mode to another by pressing the Ctrl + G + Spacebar.



To control the windows use the following keyboard shortcuts:





There is one interesting function in dvtm, which is no longer in any of the known terminal multiplexers: pressing the keys in the current window can be automatically played in all other open windows. Press the key combination Ctrl + G + A. If after that you press any key, pressing will work in all windows at once. (UPD. Our readers reported that a similar opportunity exists in tmux: tmux set synchronize-panes on).



If you enter any command in the current window, it will also be executed simultaneously in all open windows. Using this function, you can, for example, simultaneously configure several remote servers in the same way.



Unlike tmux, dvtm does not support saving and resuming sessions. To do this, use dtach, an auxiliary utility that provides the ability to disconnect from the current session and then connect to it for any console applications. Installed using the standard package manager.



To be able to disconnect sessions and reconnect to them, you need to run dvtm under dtach:



  $ dtach -c / tmp / dvtm-session -r winch dvtm 




When working with dtach, you need to explicitly specify the socket with which the session is identified. Access to the session is entirely determined by access to the socket.



To leave dtach, you need to press the combination Ctrl + \. Connection to an existing session is carried out using the command:



  $ dtach -a / tmp / dvtm-session -r winch 




More information about dtach is presented on the official man page .



In conclusion, we give a small cheat sheet - a table of basic commands and keyboard combinations used in screen, tmux and dvtm.



Comparative table of commands and keyboard shortcuts





Session Management



Operationscreentmuxdvtm
Start a new sessionscreentmux

tmux new-session

tmux new-session -s [session name]
The ability to save and resume sessions is not supported - for this you need to install the dtach utility that implements this feature for all console applications. Run dvtm under dtach using the command:

dtach -c / tmp / dvtm-session -r winch dvtm

Connect to an already created sessionscreen -rtmux attach

tmux attach-session -s [session name]

tmux -a
dtach -a / tmp / dvtm-session -r winch
Disconnect from the current sessionCtrl + ADtmux detach OR Ctrl + BDCtrl + \
View a list of active sessionsscreen -lstmux lsps aux | grep dtach
Delete sessionscreen -r [session name] -X quittmux kill-session -t [session name]-
Delete all sessionsCtrl + A \tmux kill-server-




Window management



Operationscreentmuxdvtm
Create a new windowCtrl + ACCtrl + BCCtrl + GC
Go to the specified windowCtrl + A 0 .... 9Ctrl + B 0 .... 9Ctrl + G 0 .... 9
Go to previous windowCtrl + APCtrl + BPCtrl + GJ
Go to the next windowCtrl + ANCtrl + BNCtrl + GK
Go to the last active windowCtrl + ALCtrl + BL-
Show list of all open windowsCtrl + A "Ctrl + BW-
Increase the width of the window-Ctrl + B Ctrl + N → or Ctrl + B Alt + → (increases with a big step)Ctrl + GL
Reduce the width of the window-Ctrl + B Ctrl + ←Ctrl + GH
Play keystrokes in all open windows-tmux set synchronize-panes onCtrl + GA
Close the current windowCtrl + AXCtrl + BXCtrl + GX




Readers who are not able to comment on posts on Habré, we invite to our blog .

Source: https://habr.com/ru/post/209130/



All Articles