Tmux use tutorial

Tmux is an end reuser (terminal multipliplexer) which is very useful and a common development tool. This paper describes how to use Tmux.

I. Tmux, what is it?

1.1 Sessions and processes

The command line is typically used by opening a terminal window (hereinafter referred to as “window” ) in which the command is entered.This temporary interaction between the user and the computer is called a “session” . An important feature of the session is that the window and the process initiated therein areConnected.. Opens the window, starts the session; closes the window, ends the session and ends the process within the session, whether or not it’s running out. A typical example is,SSH LoginRemote computer, open a remote window to execute the command. This is when the network suddenly breaks off and logs in again, it does not recover the last order. Because the last SSH session has been terminated and the process in it has disappeared. To solve this problem, the sessions and windows can be “dissociated”: when the window closes, they do not stop, but continue to run until later, if necessary, they are bound by other windows.

1.2 The role of Tmux

Tmux is the “de-coupling” tool for sessions and windows, which completely separates them.

(1) It allows multiple sessions to be visited in a single window at the same time. This is useful for running multiple command line programs simultaneously. (2) It allows new windows “to access” already existing ones. (3) It allows each session to have multiple connected windows and thus allows many people to share them in real time. (4) It also supports any vertical and horizontal split of windows.

A similar terminal reuser is GNU Screen. Tmux functions like it, but is more user-friendly and powerful.

II. Basic usage

2.1 Installation

Tmux usually needs to install itself.


# Ubuntu 或 Debian
$ sudo apt-get install tmux

# CentOS 或 Fedora
$ sudo yum install tmux

# Mac
$ brew install tmux

2.2 Commencement and withdrawal

After installation complete, typetmuxcommand, enter the Tmux window.


$ tmux

The above command will activate the Tmux window, with a status bar at the bottom. The left side of the status bar is window information (number and name) and the right side is system information.

PressCtrl+dOr a visible input.exitcommand, you can exit the Tmux window.


$ exit

2.3 Prefix keys

The Tmux window has a large number of shortcuts. All shortcuts are called through prefix keys. The default prefix key isCtrl+bI’m gonna press it first.Ctrl+b. For example, the shortcut to help commands isCtrl+b ?. It’s used to press first in the Tmux windowCtrl+bPress again.?, help information will be displayed. Then press ESC orqkey, you can exit the help.

III. Session management

3.1 New Session

First start-up Tmux window, number0, the number of the second window is1, and so on. These windows correspond to the session, which is 0-to-do, 1-to-be. Use numbering to distinguish between sessions, less intuitive, and a better way to name them.


$ tmux new -s <session-name>

Commands above to create a new session with the given name.

3.2 Separation session

In the Tmux window, pressCtrl+b dOr enter.tmux detachcommand, separates the current session from the window.


$ tmux detach

When the above command is executed, you will exit the current Tmux window, but the session and the process in it are still running backstage.

tmux lscommand to view all Tmux sessions.


$ tmux ls
# or
$ tmux list-session

3.3 Access sessions

tmux attachcommand to re-enter an existing session.


# 使用会话编号
$ tmux attach -t 0

# 使用会话名称
$ tmux attach -t <session-name>

3.4 Kill Session

tmux kill-sessionCommand used to kill a session.


# 使用会话编号
$ tmux kill-session -t 0

# 使用会话名称
$ tmux kill-session -t <session-name>

3.5 Switch Sessions

tmux switchcommand to switch sessions.


# 使用会话编号
$ tmux switch -t 0

# 使用会话名称
$ tmux switch -t <session-name>

3.6 Rename Session

tmux rename-sessioncommand for renaming session.


$ tmux rename-session -t 0 <new-name>

The above command renames session 0.

3.7 Session Shortcut Keys

Below are the shortcuts associated with some sessions.

  • Ctrl+b d: Separates the current session.
  • Ctrl+b s: Lists all sessions.
  • Ctrl+b $: Rename the current session.

IV. Simplified operating procedures

In summary, the following is the simplest operating process for Tmux.

  1. New Sessiontmux new -s my_session。
  2. Runs the required program in the Tmux window.
  3. Press ShortcutsCtrl+b dIt’s a separation.
  4. Reconnect to Session next time you usetmux attach-session -t my_session。

V. Window Operations

Tmux can divide windows into panes, each of which runs different commands. The following commands are executed in the Tmux window.

5.1 Split the pane

tmux split-windowCommand to divide the pane.


# 划分上下两个窗格
$ tmux split-window

# 划分左右两个窗格
$ tmux split-window -h

5.2 Mobile Cursor

tmux select-panecommand to move the cursor position.


# 光标切换到上方窗格
$ tmux select-pane -U

# 光标切换到下方窗格
$ tmux select-pane -D

# 光标切换到左边窗格
$ tmux select-pane -L

# 光标切换到右边窗格
$ tmux select-pane -R

5.3 Swap pane position

tmux swap-paneCommands are used to exchange the slot position.


# 当前窗格上移
$ tmux swap-pane -U

# 当前窗格下移
$ tmux swap-pane -D

5.4 Pane Shortcuts

Here are some shortcuts for the pane operation.

  • Ctrl+b %: Splits two windows around.
  • Ctrl+b ": Splits up and down two panes.
  • Ctrl+b <arrow key>: Cursor Switches to another pane.<arrow key>is the direction key to which you want to switch, for example, to the lower window, press the direction key.↓。
  • Ctrl+b ;: Cursor Switches to the previous pane.
  • Ctrl+b o: Cursor switch to the next pane.
  • Ctrl+b {: The current pane swaps with the previous one.
  • Ctrl+b }: The current pane swaps with the next one.
  • Ctrl+b Ctrl+o: All windows move one position forward and the first window becomes the last.
  • Ctrl+b Alt+o: All windows move one position backwards and the last window becomes the first.
  • Ctrl+b x: Close the current window.
  • Ctrl+b !: Splits the current window into a separate window.
  • Ctrl+b z: The current pane is full screen, and once again it will be back to its original size.
  • Ctrl+b Ctrl+<arrow key>: Resize the pane by arrow direction.
  • Ctrl+b q: Show the pane number.

VI. Window management

Apart from dividing a window into multiple pane, Tmux allows for the creation of new windows.

6.1 New Window

tmux new-windowcommand to create new window.


$ tmux new-window

# 新建一个指定名称的窗口
$ tmux new-window -n <window-name>

6.2 Toggle Window

tmux select-windowcommand to switch the window.


# 切换到指定编号的窗口
$ tmux select-window -t <window-number>

# 切换到指定名称的窗口
$ tmux select-window -t <window-name>

6.3 Rename Window

tmux rename-windowcommand to name (or rename) the current window.


$ tmux rename-window <new-name>

6.4 Window Shortcuts

Here are the shortcuts for some window operations.

  • Ctrl+b c: Creates a new window, and the status bar displays information from multiple windows.
  • Ctrl+b p: Switch to the previous window (in order of status bar).
  • Ctrl+b nSwitch to the next window.
  • Ctrl+b <number>: Switch to the window with the specified number, where<number>is the window number on the status bar.
  • Ctrl+b w: Select the window from the list.
  • Ctrl+b ,: Window rename.

VII. OTHER ORGANS

Here are some other orders.


# 列出所有快捷键,及其对应的 Tmux 命令
$ tmux list-keys

# 列出所有 Tmux 命令及其参数
$ tmux list-commands

# 列出当前所有 Tmux 会话的信息
$ tmux info

# 重新加载当前的 Tmux 配置
$ tmux source-file ~/.tmux.conf

VIII. REFERENCES

(concluded)

未经允许不得转载:萌萌Apprentissage » Tmux use tutorial

Like (0) 打赏

Comments 0

Protected with IP Blacklist CloudIP Blacklist Cloud
  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏

//下一个脚本特效
//下一个脚本