Detailed explanation of tmux terminal multiplexing

what is tmux

Our work on the Linux server is generally performed by connecting to the remote system through a remote terminal connection software, such as using xshell or SecureCRT tool to connect remotely through ssh. In the process of use, if you want to do time-consuming operations, such as sometimes compiling, or downloading large files, it takes a long time. Generally, you can run it directly after get off work and hope to come and complete the operation the next morning, so that you don’t need to Delay working time. However, the network is sometimes unstable, and the connection may be disconnected in the middle of the night. Once the connection is disconnected, the program we execute will be interrupted. Of course, we can write a script to run in the background, but it is still inconvenient. So is there a tool that can solve such a problem? This is the tmux we are going to mention here. In fact, there are many tools similar to tmux. For example gnu screen etc. tmux can just solve the problem we described. When we work in tmux, even if we close the SecureCRT connection window, connect again, and enter the tmux session, our previous work continues.

tmux is a tool under linux and needs to be installed before using it, just like installing other tools under linux. First, we log in to the remote linux machine through the SecureCRT connection. We call the environment at this time the terminal environment. If tmux is not installed on this machine, we need to install it. For example, on CentOs, it is yum install tmux. After that, we can use the tmux command. There are three concepts in tmux, session, window, and pane. A session is a bit like a tmux service, running on the backend, we can create this service through the tmux command, and can view it through the tmux command, and attach to the session running on the backend. A session can contain multiple windows, and a window can be divided into multiple panes. First let's take a look at the tmux session.

tmux session

1. tmux new -s session1 to create a new session

Use the tmux new -s command to create a new session -s (actually the first letter of the session). You can specify the session name later. After running, it will enter the session environment from the terminal environment of the shell, and stay in the newly created session. E.g:

session

You can see the display after entering the session. There is a green status indicator bar below. The name of the current session is displayed on the left, followed by the window number and window name in the session. We will talk about the concept of windows later. There is an asterisk * after the window name to indicate that it is the current window we are operating on. There can be multiple windows in a session. When entering a session, a window is automatically created. As shown in the image above, the above environment is referred to as the session environment in this chapter. In this way, we have started the use of tmux. If you close the SecureCRT software at this time, the session will still be running when you enter it next time. That is to say, when we use wget to download a large file in the session environment we just entered, or compile a very time-consuming project, we close the connection of the SecureCRT, and then enter the next time, the session still exists, and the session runs The compile command or wget download command is still running and will not be terminated by closing SecureCRT, which is exactly what we need. Not affected by SecureCRT network connections. Even we can turn off the entire SecureCRT program.

2. ctrl+bd to exit the session and return to the terminal environment of the shell

We just created a tmux session through the tmux new -s command and entered the session. If we want to exit the session environment and return to the terminal environment (the programs in the session will not exit and keep running in the background). How should we do it? For example, in the picture above, we currently use a shortcut key ctrl+bd in the tmux session environment (press ctrl+b and then press a letter d, the letter d is the abbreviation of detach). The result after the operation is as follows:

detach

You can see that the green status bar has disappeared, and a [detached] appears at the top, indicating that it has left the tmux session and is no longer returning to the shell terminal environment in the tmux session environment.

It is necessary to say here that in the tmux session environment, we often use the key combination of tmux. Generally, a prefix is ​​added to the key combination, that is, ctrl+b. In addition, in the following description, the terminal environment we say refers to After using SecureCRT to enter the remote linux but not the state of the session environment of tmux.

After the above operation ctrl+bd, return to the terminal environment, in fact, the tmux session is still running in the background, how to check it.

3. tmux ls terminal environment to view the session list

In the terminal environment, we can view the session list of tmux running in the background through the tmux ls command, for example:

tmuxls

It can be seen that there is only 1 row in the list, indicating that there is only one session. The session1 on the left represents the name of the session, the 2 windows in the middle indicate that there are 2 windows in the session1 session, and the right side represents the time when the session was created. If there are multiple tmux sessions running in the background on that machine, there will be multiple lines listed here. Since tmux sessions are running in the background, we guess that there must actually be tmux processes running in the background to maintain these sessions. We can ps take a look:

pstmux

You can see that the command we used to create the session is still running in the backend.

4. ctrl+bs session environment to view the session list

In the above command, we have exited the tmux session environment, and in the terminal environment, use tmux ls to list the list of tmux sessions running in the background of the current linux machine. Then assuming that our current environment is already in the tmux session environment, how do we get the current tmux session list? If we have to exit the current session every time, it is very inconvenient to return to the shell terminal environment and then run tmux ls to view, then In the session environment of tmux, we can use ctrl+bs to get a list of all background sessions of tmux on the current linux machine. For example, the operation is displayed as follows:

sessionls

At this point, you can use the arrow keys to select a session and press Enter to switch between sessions.

5. tmux a -t session1 to enter the session from the terminal environment

If you run tmux ls in the terminal environment to see that a tmux session is running in the background, how to enter the session running in the background? You can enter the existing session session1 by running tmux a -t session1. The a letter is the first letter of attach, which means to attach, -t specifies to enter the existing session name, if it does not exist, it will report a session not found error.

6. tmux kill-session -t session1 destroy session

We can destroy sessions in the terminal environment and session environment, for example, run tmux kill-session -t session1 in the terminal environment to end the tmux session named session1.

Run in the session environment ctrl+b : (note that press a colon key after pressing the key combination), after the status bar turns yellow, it prompts us that we can enter commands in the session environment. At this time, enter kill-session -t session1 and press Enter. where session1 is the session name to be destroyed.

After the session is destroyed, run tmux ls in the terminal environment or run ctrl+bs in the session environment and the destroyed session will no longer appear in the session list.

tmux windows

A tmux session can have multiple windows (windows), and each window can be divided into multiple panes (panes). The smallest unit of our work is actually the pane. By default, in a window, there is only one large pane, occupying the entire window area. We work in this area.

In this section, we will explain the related operations of the tmux window, and then we will talk about the related knowledge of the pane (pane). First of all, a window will be created by default in a newly created session. As the diagram we mentioned above, it looks like this:

session

A window will be created by default in the newly created session. The name of the window is generally the username@hostname of the login terminal. We can crtl+b , modify the name of the current window by (pressing a comma after the key combination), as shown in the window name above. myserver1 is the name after the modification. There is a * sign after the name, indicating that the window is the active window (keyboard input will be entered into this window)

1. Create a window

You can create multiple windows in the current session window. For example, after ctrl+bc is created, an additional window will appear as shown in the following figure:

create_window

By default, the window created is composed of the window number + the window name. The window name can be modified by the method mentioned above. You can see that the newly created window has an * sign behind it, indicating that it is the current window.

2. Switch windows

The following shortcut keys can be used to switch between multiple windows in the same session:

ctrl+bp (the first letter of previous) switches to the previous window.

ctrl+bn (the first letter of next) switches to the next window.

ctrl+b 0 switch to window 0, and so on, can be replaced with any window number

ctrl+bw (the first letter of windows) List all windows of the current session, switch windows by the up and down keys

ctrl+bl (lowercase letter L) adjacent windows switch

3. ctrl+b & close the window

ctrl+b & close the current window, it will prompt whether to close the current window, press y to confirm.

tmux pane

A window of tmux can be divided into multiple panes, which can make a split screen effect.

1. ctrl+b % to split the screen vertically (press a percent sign after the key combination), divide the current window into left and right screens with a vertical line.

pan

2. ctrl+b "Horizontal split screen (press a double quotation mark after the key combination), use a horizontal line to divide the current window into upper and lower screens.

pane_h

Which pane the cursor stays on after the split screen indicates that the pane is active. In addition, the current pane will be surrounded by green lines in general. Generally, the current window name will be reset to the default window name after split screen. Through multiple split-screen operations, we can obtain various split-screen effects. For example, the following figure shows the effect of horizontal splitting in the right pane after a vertical splitting:

pane_multi

You can see that the split screen in the lower right corner is a green box, indicating that it is the current active pane

3. Switch pane

ctrl+bo switches the panes under the current window in turn.

ctrl+b Up|Down|Left|Right Switch to a pane according to the arrow direction selection.

ctrl+b Space (space bar) Rearrange the layout of all panes under the current window, and change a style each time you press it.

ctrl+bz maximizes the current pane. Press again to resume.

4. Close the pane

ctrl+bx closes the currently in use pane, after the operation will give a prompt whether to close, press y to confirm and close.

Summarize

The three most important concepts in tmux, the use of windows and panes, have been introduced. In fact, this is the most commonly used function for us to operate tmux. If you master it well, it is enough to handle most of the work. In addition, tmux has some advanced usages, such as the ability to customize its key combination (the official default ctrl+b key combination is not very convenient to press and can be modified, UI settings, mouse support, copy and paste, etc.), but I think these advanced The function is basically useless. If necessary, you can check the relevant information by yourself.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324628617&siteId=291194637