Join Our Telegram GroupsTelegram

how to use Tmux

Tmux is a terminal multiplexer (terminal multiplexer), very useful, it is a commonly used development tool.

This article describes how to use Tmux.

Tmux tutorial illustration

1. What is Tmux?

1.1 Sessions and processes

The typical way to use the command line is to open a terminal window (terminal window, hereinafter referred to as "window") and enter commands in it. This temporary interaction between the user and the computer is called a "session"  .

An important feature of the session is that the window is connected to the process started in it. When the window is opened, the session begins; when the window is closed, the session ends, and the processes within the session will also terminate, regardless of whether it has run out or not.

A typical example is to log in to a remote computer via SSH and open a remote window to execute commands. At this time, the network is suddenly disconnected, and when logging in again, the last executed command cannot be retrieved. Because the last SSH session has been terminated, the processes inside have also disappeared.

In order to solve this problem, the session and the window can be "unbound": when the window is closed, the session does not terminate, but continues to run. When needed in the future, let the session "bind" to other windows.

1.2 The role of Tmux

Tmux is a tool for "unbinding" sessions and windows, completely separating them.

(1) It allows simultaneous access to multiple sessions in a single window. This is useful for running multiple command line programs at the same time.

(2) It allows the new window to "access" an existing session.

(3) It allows multiple connection windows per session, so multiple people can share sessions in real time.

(4) It also supports arbitrary vertical and horizontal splitting of windows.

A similar terminal multiplexer is GNU Screen. Tmux has similar functions, but is easier to use and more powerful.

2. Basic usage

2.1 Installation

Tmux generally needs to be installed by yourself.


# Ubuntu Debian
$ sudo apt-get install tmux
# CentOS Fedora
$ sudo yum install tmux
# Mac
$ brew install tmux

2.2 Start and exit

After the installation is complete, type the tmuxcommand to enter the Tmux window.

  1. $ tmux

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

Press Ctrl+dor explicitly enter the exitcommand to exit the Tmux window.

  1. $ exit

 2.3 Prefix key

The Tmux window has a large number of shortcut keys. All shortcut keys must be invoked by the prefix key. The default prefix key is Ctrl+b, that is , press it first Ctrl+b, the shortcut key will take effect.

For example, the shortcut key for the help command is Ctrl+b ?Its usage is that in the Tmux window, press first Ctrl+b, then press ?, and the help information will be displayed.

Then, press the ESC key or qkey to exit the help.

Three, session management

3.1 New session

The number of the first Tmux window started is, the number of the 0second window is 1, and so on. The sessions corresponding to these windows are session 0 and session 1.

Using numbers to distinguish conversations is not intuitive, and a better way is to name the conversation.

  1. $ tmux new -s <session-name>

 The above command creates a new session with the specified name.

3.2 Separate Session

In the Tmux window, pressing Ctrl+b dor entering a tmux detachcommand will separate the current session from the window.

  1. $ tmux detach

 After the above command is executed, the current Tmux window will be exited, but the session and the processes inside are still running in the background.

tmux lsCommand can view all current Tmux sessions.

  1. $ tmux ls
    # or
    $ tmux list-session

 3.3 Access session

tmux attachThe command is used to reconnect to an existing session.


$ tmux attach -t 0
$ tmux attach -t <session-name>

3.4 Kill the session

tmux kill-sessionThe command is used to kill a session.


$ tmux kill-session -t 0
$ tmux kill-session -t <session-name>

3.5 Switch session

tmux switchCommands are used to switch sessions.

  1. $ tmux switch -t 0
    $ tmux switch -t <session-name>

 

3.6 Rename session

tmux rename-sessionThe command is used to rename the session.

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

 

The above command renames session 0.

3.7 Session shortcuts

The following are some shortcut keys related to the session.

  • Ctrl+b d: Separate the current session.
  • Ctrl+b s: List all conversations.
  • Ctrl+b $: Rename the current session.

Four, the simplest operation process

In summary, the following is the simplest operation process of Tmux.

  1. Create a new conversation tmux new -s my_session.
  2. Run the required program in the Tmux window.
  3. Press the shortcut key Ctrl+b dto separate the conversation.
  4. Next time you use it, reconnect to the session tmux attach-session -t my_session.

Five, pane operation

Tmux can divide the window into multiple panes, and each pane runs different commands. The following commands are executed in the Tmux window.

5.1 Split pane

tmux split-windowCommands are used to divide the panes.


$ tmux split-window
$ tmux split-window -h


5.2 Move the cursor

tmux select-paneCommands are used to move the cursor position.

  1. $ tmux select-pane -U
    $ tmux select-pane -D
    $ tmux select-pane -L
    $ tmux select-pane -R

 5.3 Swap the position of the pane

tmux swap-paneCommands are used to swap the positions of the panes.

  1. $ tmux swap-pane -U
    $ tmux swap-pane -D


        5.4 Pane shortcuts

Below are the shortcut keys for some pane operations.

  • Ctrl+b %: Divide the left and right panes.
  • Ctrl+b ": Divide the upper and lower panes.
  • Ctrl+b <arrow key>: The cursor switches to other panes. <arrow key>It is the arrow key that points to the pane to be switched to. For example, to switch to the lower pane, press the arrow key .
  • Ctrl+b ;: The cursor switches to the previous pane.
  • Ctrl+b o: The cursor switches to the next pane.
  • Ctrl+b {: The current pane and the previous pane swap positions.
  • Ctrl+b }: The current pane and the next pane swap positions.
  • Ctrl+b Ctrl+o: All panes move forward one position, and the first pane becomes the last pane.
  • Ctrl+b Alt+o: All panes move one position backward, and the last pane becomes the first pane.
  • Ctrl+b x: Close the current pane.
  • Ctrl+b !: Split the current pane into an independent window.
  • Ctrl+b z: The current pane is displayed in full screen, and it will return to its original size when used again.
  • Ctrl+b Ctrl+<arrow key>: Adjust the size of the pane in the direction of the arrow.
  • Ctrl+b q: Display the pane number.

Six, window management

In addition to dividing a window into multiple panes, Tmux also allows multiple windows to be created.

6.1 New window

tmux new-windowThe command is used to create a new window.


$ tmux new-window
$ tmux new-window -n <window-name>

6.2 Switch window

tmux select-windowCommands are used to switch windows.

  1. $ tmux select-window -t <window-number>
    $ tmux select-window -t <window-name>

        

6.3 Rename window

tmux rename-windowThe command is used to name (or rename) the current window.

  1. $ tmux rename-window <new-name>

 

6.4 Window shortcuts

The following are some shortcut keys for window operations.

  • Ctrl+b c: Create a new window, and the status bar will display the information of multiple windows.
  • Ctrl+b p: Switch to the previous window (according to the order on the status bar).
  • Ctrl+b n: Switch to the next window.
  • Ctrl+b <number>: Switch to the window with the specified number, which <number>is the window number on the status bar.
  • Ctrl+b w: Select the window from the list.
  • Ctrl+b ,: Rename the window.

Seven, other orders

Here are some other commands.

  1. # List all shortcut keys and their corresponding tmux commands

    $ tmux list-keys
    # List all Tmux commands and their parameters$ tmux list-commands
    $ tmux info
    $ tmux source-file ~/.tmux.conf

Post a Comment

Hope you enjoyed the article!😊
Post a Comment