A few minutes before leaving get off work, I completely abandoned tmux

1. Introduction to tmux

tmux (terminal multiplexer) is a very powerful tool, mainly has the following functions:

  • Terminal multiplexing: tmux enables you to run and manage multiple terminals in a single window or terminal session. Not only does this allow you to quickly switch between different tasks, but it also allows multiple tasks to be displayed side by side on the same screen. For example, you may wish to run a text editor in one window and a server in another, or you may need to monitor multiple log files simultaneously. tmux makes all of this possible.
  • Session management: tmux allows you to create, access and control multiple sessions. Each session is a completely independent workspace that can contain multiple windows and panes. This means that you can work on one task in a separate session, such as developing a specific project, and another task in another session, such as administering the system. This way makes organizing work much easier.
  • Persistence: When your SSH connection drops, or your network connection fails, tmux will still keep running. All sessions, windows and panes are preserved, as if you never left. When you reconnect, you can resume where you left off and continue your work without losing anything.
  • Collaboration: With tmux, multiple users can work in the same session. This allows team members to share their endpoints for collaborative editing, or troubleshooting.

Install tmux:

# Ubuntu
sudo apt-get install tmux

# MacOS
brew install tmux

We first need to understand the three major concepts of tmux: Session (session) , Window (window) and Pane (pane) .

For the convenience of understanding, you can simply think that Session is a browser, Window is the tab page, and Pane is the workspace under the tab page (it seems that there is no concept of workspace under the tab page, but it can only be explained forcefully . ).

Type the command in the command line tmuxto create a new session, and observe the status bar below, which has the following form:

As you can see, for a session, it only has the concept of a name. As for the window, it has the two concepts of number and name at the same time. *Represents the current window -and the previous window.

While in a session, type exitor press Ctrl + dto exit the session (the session is also deleted).

2. Session

Execute directly on the command line

tmux

You can quickly create a new tmux session. The name of the session defaults to 已存在的会话数. For example, if two sessions currently exist, the new session name will be 2.

The following table lists commonly used session-related commands:

Order effect
tmux new -s [会话名称] Create a new session with a custom name
tmux rename-session -t [旧会话名称] [新会话名称] rename an existing session
tmux detach detach from current session
tmux ls list all conversations
tmux a -t [会话名称] Reconnect to an existing session (a means attach)
tmux kill-session -t [会话名称] kill a session
tmux kill-session -a kill all sessions
tmux switch -t [会话名称] Switch to a session (the command must be executed in the session)

Next comes the most critical part of tmux- shortcut keys . Shortcut keys are composed of prefix keys and command keys , and can only be used in tmux sessions .

The prefix key of tmux is that Ctrl + bto use the shortcut key, you should press the prefix key first, and then press the command key, instead of pressing the prefix key and the command key together.

The following table lists commonly used session- related shortcut keys ( prefix keys are omitted here, the same below ):

command key effect
$ rename current session
s List all sessions (arrow keys to switch sessions up and down, expand the windows in sessions left and right)
d detach from current session

For example, to break out of the current session, press first Ctrl + b, then press d, not all together Ctrl + b + d.

3. Window

The following table lists commonly used window- related shortcut keys:

command key effect
c create a new window
, rename current window
& delete current window
0-9中的一个数字 Jump to the corresponding window according to the window number
w List all windows (if the window number is greater than 9, you can jump in this way)

4. Pane

The following table lists commonly used pane- related shortcut keys:

command key effect
% Split a pane horizontally
" Split a pane vertically
x delete current pane
0-9中的一个数字 Jump to the corresponding window according to the window number
w List all windows (if the window number is greater than 9, you can jump in this way)
方向键 Jump back and forth between panes
q Show all pane numbers (press the number before it disappears to jump to the corresponding pane)
z Maximize the current pane (execute the same operation after maximizing to restore to the original)

5. Customize tmux configuration

The tmux configuration file is here ~/.tmux.conf, here is a blogger’s own configuration:

# 启用鼠标模式
# 可使用鼠标点击以在窗格中跳转
# 也可以使用鼠标改变窗格大小
set -g mouse on

# 设置历史命令缓存为 20000
set -g history-limit 20000

# 设置状态栏背景色为品红色
set -g status-bg magenta
# 设置状态栏文字颜色为白色
set -g status-fg white
# 设置状态栏左侧显示会话名称
set -g status-left "[#S] "
# 设置状态栏右侧显示当前日期和时间
set -g status-right "%Y-%m-%d %H:%M"

# 设置当前窗口状态显示格式
set -g window-status-current-format '#[fg=black](#I:#W)'
# 设置非当前窗口状态显示格式
set -g window-status-format '#[default]#I:#W'
# 设置窗口自动重命名
setw -g automatic-rename

# 设置快捷键 Ctrl + h/j/k/l 来切换窗口面板
# -n代表不需要按前缀键
bind -n C-h select-pane -L
bind -n C-j select-pane -D
bind -n C-k select-pane -U
bind -n C-l select-pane -R

# 按下命令键y后,可以在所有窗格同步进行输入
bind y setw synchronize-panes

# 按下命令键r后,重新加载tmux配置
bind r source-file ~/.tmux.conf \; display "Config reloaded!"

After configuration, we need to execute

tmux source-file ~/.tmux.conf

to make the configuration file take effect. But notice that we have already configured shortcut keys, so we only need to press first in the tmux session Ctrl + b, and then press rto load the new configuration.

If the configuration is still not loaded successfully, execute the following command

tmux kill-server

6. Manipulating tmux in a shell script

In shell scripts, we usually add -dto create a tmux session in the background (this ensures that the session will not be entered immediately after the session is created):

tmux new -ds [会话名称]

After creating a session, use to send-keyssend a key to the specified session (note that it is a key), the syntax is as follows:

tmux send-keys -t [会话名称] [按键1] [按键2] ...

For example

tmux new -ds abc
tmux send-keys -t abc pwd
tmux a -t abc

It can be seen that the command has not been executed

This is because send-keysthe function of is only to send the key. To execute the command, we also need to send an Enter key at the end, that is, to modify the second behavior

tmux send-keys -t abc pwd Enter

For commands containing spaces, we can use strings to wrap them, or use Spacekeys. The following two commands are equivalent:

tmux send-keys -t abc "ls -l" Enter
tmux send-keys -t abv ls Space -l Enter

We can also send Ctrl + cto interrupt an executing command:

tmux send-keys -t abc C-c

If there are multiple windows in the session, and each window has multiple panes, the above keystrokes will be sent to the current active pane of the current active window of the session by default . To specify a window pane to send, use the following syntax:

tmux send-keys -t [会话名称]:[窗口编号/窗口名称].[窗格编号] [按键1] [按键2] ...

References

[1] https://github.com/tmux/tmux/wiki/Getting-Started
[2] https://www.cnblogs.com/zuoruining/p/11074367.html

Guess you like

Origin blog.csdn.net/raelum/article/details/131613773