Tmux tutorial (1): a preliminary understanding of the terminal multiplexer

  The next few articles are about the multiplexer of the Linux desktop terminal. Let’s start with Tmux. The article is mainly translated from the FOSS LINUX website. If there is any infringing version, please contact us in time. We will deal with it as soon as possible. The most important thing is that we do not translate 100% of the original text, but organize it as needed so that it is easier for everyone to understand.
  Tmux allows us to manage multiple terminal sessions in a single window, thereby improving the efficiency of Linux system operation and maintenance and other management work. Then we start with the basic commands of Tmux, and hope that everyone can grow into a master of Tmux through this tutorial .
insert image description here
  Many friends who like Linux will work in the command line mode, and often encounter situations such as frequent switching of terminals, running multiple commands, loss of process tracking, etc., so it is necessary to use Tmux to solve such needs. Tmux is actually a terminal multiplexer that allows us to run multiple terminals in one window, disassemble and reassemble sessions, and share terminal sessions with other users. This article will tell you how to use the basic commands of Tmux, so as to improve the efficiency of our daily production work.

  Regarding the translation of relevant terms in Tmux, in various Chinese materials, such as attach, sometimes translated as attachment, hook, sometimes translated as combination, etc., detach is sometimes translated as separation, detachment, and sometimes translated as decoupling. Personally, I think this is not convenient. Understand some concepts and practical applications of Tmux, and consider that Tmux is a terminal multiplexer, which is similar to the disassembly and assembly of electromechanical devices. It is based on the terminal body and combines and reassembles components to maximize The performance of the terminal, so this article refers to electromechanical and other Chinese materials about Tmux, and tentatively translates attach as assembly, detach as disassembly, and reattach as reassembly.

Tmux Preliminary Explanation: Basic Commands for Terminal Multiplexing

  What is Tmux?
  In fact, Tmux means terminal multiplexer. Through this command line tool, we can create and manage multiple terminal sessions in one terminal interface. Tmux is obviously particularly useful in remote sessions, where we can continue to run an operating session after the actual network connection has been disconnected. In addition, we can also disassemble and reassemble the session of the terminal, and even start a session in a certain machine, then disassemble the session, and finally reassemble the session on another machine.
  The working mechanism of Tmux is actually similar to GNU Screen, another terminal multiplexer. We will also explain GNU Screen and the difference between Tmux and it in future articles. However, we know that Tmux provides more advanced functions than other multiplexers, such as supporting multi-pane processing of a single window, multi-user session sharing of client-server architecture, and so on.

Install Tmux

  Before we start explaining the basic commands of Tmux, we need to install Tmux first. Most Linux distributions provide Tmux. Tmux can be installed using its package manager, for example, in Ubuntu or other Debian-based distributions. , we can run the following code to install Tmux:

sudo apt-get install tmux

Install Tmux on Ubuntu
  For Fedora or Red Hat-based distributions, use the following code to install Tmux:

sudo dnf install tmux

  For Arch Linux, it is:

sudo pacman -S tmux

  or

yay -S tmux

  If we are using the macOS operating system, Tmux can be installed through the Homebrew command:

brew install tmux

  After completing the installation of Tmux, we can enter the topic to explain the basic commands of Tmux.

conversation

  Tmux session is one of the functions of terminal multiplexing, which allows us to create and manage multiple terminal sessions in one terminal interface, and disassemble and assemble these sessions, so we can get rid of those that need to run in the background for a long time Task-bound to move on to other sessions, or even shut down the computer.

  In other words, a Tmux session is like a container that can hold multiple windows, which in turn hold multiple panes. We can switch between different tasks or projects in the same terminal window without opening multiple terminal windows or multi-tab pages. Developers, system administrators, or anyone else who works long hours at the command line interface will especially appreciate this feature.

Create a new Tmux session

  The first command we will learn is to create a new Tmux session. To create a new session, just type in the terminal:

tmux new -s session-name

  The above code will create a session named "Session-name", of course you can also enter the complete command code:

tmux new-session -s session-name

Note: We can specify a session name instead of the above "session-name" session name. If no name is specified, Tmux will automatically create a name. For example, to create a session named "foss-linux", the code is :

tmux new -s foss-linux

  At this time, we will see a green status bar appearing at the bottom of the terminal window. Of course, we can also configure the color of the status bar. For example, the color of the status bar in the figure below is yellow. In addition to the name of the session, the status bar also displays the machine's hostname, date and time, and the current working directory.
Tmux new session

Switch Tmux sessions

  To switch Tmux sessions, you can use the command:

tmux switch -t session-name

  The above command will switch to the session-name session.
Example :
  For example, if we want to switch to "foss-linux", then we can execute the following code:

tmux switch -t foss-linux

Tmux session switching
  Execute the following code to switch to the next session without specifying a session name:

tmux switch-client -n

switch to next session
  We see that the above code can also switch to the session-name session. Of course, contrary to the above code, we can also switch to the previous session:

tmux switch-client -p

switch to previous session

Disassemble and assemble a Tmux session

  One of the most powerful features of Tmux is the ability to disassemble and reassemble across multiple sessions. Disassembling a session allows a specific session to continue running in the background, and we can do another task without being restricted by a specific session. To tear down a session use the following code:

tmux detach

  This command will tear down the current session and return to the shell's terminal prompt.

teardown session
  To assemble a specific session, we need to know its name first, and list the running sessions with the following command:

tmux ls

  This command will list all Tmux sessions, including the session name and status.
print session
  To assemble a session, enter:

tmux attach -t session-name

  This command will reassemble the session named "session-name". If we have multiple sessions running, we can switch to the specified session with the following switch command:

tmux switch -t session-name

  For example, if we want to switch to a session named "foss-linux", then:

tmux attach -t foss-linux

assembly session

Rename the Tmux session

  To rename a Tmux session, we can use the following command:

tmux rename-session -t old-name new-name

  This command will change the old session name to the new session name. For example, if we want to change "foss-linux" to "foss-linux-tuts", then the following code:

tmux rename-session -t foss-linux foss-linux-tuts

rename session

Print Tmux session

  If we want to print the entire Tmux session, we can use the built-in print command:

tmux list-sessions

  Of course, we can also use the shortcut command "tmux ls" to list all Tmux sessions.
print session

End the Tmux session

  To end a Tmux session, we can use the command:

tmux kill-session -t session-name

  It will end the session with name "session-name". For example, to end the foss-linux-tuts session, we execute the following code:

tmux kill-session -t foss-linux-tuts

end session
  We can also end all sessions with the following command:

tmux kill-session -a

Create a Tmux session from another terminal

  We open a new terminal and want to select a session from other terminals and assemble it into the current terminal to create a new Tmux session, we can use the following command:

tmux new -t foss-linux-tuts

  It will create a session named foss-linux-tuts and assemble it to the current terminal.
Create sessions from other terminals

Share Tmux session

  Sharing sessions with other users of the system is also one of the unique features of Tmux, which is very beneficial for pair programming and remote assistance. Use the following command to create a server to share sessions first:

tmux new-session -s fosslinux -d

The server to detach the session from
  It will start the server in teardown mode with a new session named "fosslinux", however to tear it down and send the session name and hostname to other users, they can run the following command to connect to the session:

tmux attach -t fosslinux

  This will assemble the Tmux session into the other user's terminal window, allowing both to work in the same shell.
Assemble a sharing session

window

  In Tmux, windows are similar to tabs in a web browser or a text editor. We can create multiple windows in a terminal interface, and each window can have its own shell, so that we can multitask. Here's how to create and switch windows in Tmux:

create window

  To create a new window, press both Ctrl-b c.
create new window
  This will create a new window with the default shell, usually the system default shell such as Bash or Zsh. We can also run
" tmux new-window -n window-name shell-command " to specify the shell, where window-name is the name of the window (optional), and shell-command is the command to run in the new window.

Note: Ctrl-bIt is the default tmux prefix.

switch window

  We can use Ctrl-b nthe and Ctrl-b pcommands to switch windows. Ctrl-b nswitches to the next window, and ctrl-b pswitches to the previous window.
switch window
  We can also run Ctrl-b [窗口编号]to switch to the specified window, [window number] is the number of the window to be switched.

rename window

  We need to switch to the window to be renamed to rename the window, and then press Ctrl-b , (comma) to enter the rename window mode.
rename window
  We can edit the name of the current window here, and press Enter to save the new window.

close the window

  To close the window, we must first make sure that we are not in the window to be closed. We first use Ctrl-b nthe or Ctrl-b pcommand to switch to other windows, and then press ctrl-b &to close the current window, closing the current window and the panes in the window.
close the window

close all windows

We can close all windows in a Tmux session with the following command:

tmux kill-session

It will close all windows and end the session. If we want to keep multiple sessions running, we also need to specify which session needs to end:

tmux kill-session -t session-name

"session-name" in the code above is the name of the session to end.

insert image description here

pane

  In Tmux, a pane is an area of ​​the screen divided within a window. We can split a window into multiple panes, and each pane has its own shell, so that we can multitask in the same window. The following shows how to create and manage pods.

create pane

  Press Ctrl-b %to split the current pane horizontally.
split pane horizontally
  We can also press ctrl-b ” (close quote) to split the current pane vertically.
split pane vertically
  Newly created panes have a default shell, usually the system default shell, such as Bash or Zsh. We can also specify shell commands:

tmux split-window -h shell-command

insert image description here

  The above command can split the window horizontally and run the specified command. Of course, you can also execute the following command to split the window vertically and run the specified command.

tmux split-window -v shell-command

split pane vertically

switch pane

  We can use Ctrl-b 方向键shortcut keys to switch panes. Ctrl-b 方向键The shortcut key can switch to the next pane according to the direction of the key. For example, ctrl-b 向左箭头you can switch the current pane to its left pane.
insert image description here

  We can also run ctrl-b q,to switch to the specified pane, it can display the number list of all panes in the current window, enter the corresponding number and press Enter to switch to the pane we want.

insert image description here

resize pane

  Ctrl-b 方向键 ,The shortcut keys can be used to adjust the size of the pane, and the shortcut keys Ctrl-b ,have the following command modes:
: resize-pane -U Increase the area of ​​the current pane upward
: resize-pane -D Increase the area of ​​the current pane downward
: resize- pane -L increases the area of ​​the current pane to the left
: resize-pane -R increases the area of ​​the current pane to the right

Note: Command mode must be started with a ( : ) colon
insert image description here

  Of course, we can also add numbers after the command to define the size of the region. For example, the "Ctrl-b : resize-pane -U 5" command can increase the area by 5 lines upwards. If the terminal supports it, we can also drag and drop the area size with the mouse.
insert image description here

move pane

  We can also move the current pane to the left through Ctrl-b { the shortcut key, and Ctrl-b }the shortcut key can move the current pane to the right. It can also be selected by Ctrl-b :entering the command mode and entering swap-pane -[U|D|L|R] [target pane], where the target pane is the number of the pane to be swapped.

close pane

Ctrl-b x.The shortcut key can close the current pane and all running processes in the pane.
insert image description here

pane split

  Another powerful feature of Tmux is the ability to split a window into multiple panes, each of which can have its own shell. Split pane horizontally, available Ctrl-b %, split the current pane into two panes horizontally.
insert image description here

  To split panes vertically use Ctrl-b “shortcut keys.
insert image description here

  We can also Ctrl-b 方向键switch panes with . For example Ctrl-b 向左方向键"move pane left.

Closing panes and windows

To close a pane, just type exit   in the shell or use Ctrl-da shortcut key. It will close the shell and remove the pane from the window.
insert image description here

To close a window, we must first make sure that we are not in the window. You can switch to other windows with Ctrl-b nor shortcut keys, and then close the window.Ctrl-b pCtrl-b &.
insert image description here

Summarize

  This article briefly introduces the use of Tmux basic commands. Tmux is a powerful tool that allows us to create and manage multiple terminal sessions in one window, disassemble and assemble sessions, share sessions with other users, and more. By learning these basic commands, we can multitask more efficiently in the command line interface. I hope that everyone can use Tmux proficiently in their work, making it a useful tool in daily work. Thanks!

Guess you like

Origin blog.csdn.net/weixin_37885187/article/details/129660417