Application scenarios

When you use SSH to log in to a remote machine and want to open one or more tabs, you need to perform SSH login again. Assuming your SSH login is not secret free, you will need to enter your password every time you log in! Assuming that you need to log in to a remote machine through one or more jumpers, the cost of opening a new Tab increases dramatically…

Oh, my God. Somebody help me? A: Tmux

Simple to use

Let’s take a brief look at Tmux in order, and then introduce some basic concepts and common shortcuts.

How do I turn on Tmux?

tmux              Create a session without specifying a session name
tmux new -s demo  Create a session and specify the session name as demo
Copy the code

How do I exit a session?

What do you do when you do something time-consuming in a session and want to exit without closing the session?

  1. Enter the following command line:tmux detach
  2. Use shortcut keysCtrl+b :And then typedetach
  3. Use shortcut keysCtrl+b d(recommended)

What do you do when you do something in a session and want to exit and close the session?

  1. Enter the following command line:tmux kill-session
  2. Use shortcut keysCtrl+b :And then typekill-session

How do I view the session list?

When you want to check which existing sessions exist, you can use the following command to check

tmux list-sessions  List all sessions
tmux ls             # alias
Copy the code

How do I enter an existing session?

When you want to enter an existing session and continue, use the tmux attach -t session_name command, alias tmux a -t session_name

tmux a          The first session is entered by default
tmux a -t demo  Enter a session called Demo
Copy the code

How do I switch from one session to another?

If you want to switch from one session to another, use the tmux switch -t session_name command

tmux a               Enter the first session
tmux switch -t demo  Switch to a session named Demo
Copy the code

The basic concept

There are several concepts in Tmux, namely Session, Window and Pane. The relationship between them is shown in the figure below:

In the Tmux service, there are multiple sessions, one Session has multiple Windows, and one Window has multiple Panes. When a Session is created, a Window is created by default, and a Pane is created by default.

Common Shortcut keys

System instructions

The prefix instruction describe
Ctrl+b ? Displays the shortcut key help document
Ctrl+b d Exit the current session
Ctrl+b D Select to exit the current session
Ctrl+b Ctrl+z Suspend the current session
Ctrl+b r Force the current session to reload
Ctrl+b s Displays the session list for selection and switching
Ctrl+b : Enter the command line interface (CLI)lsSuch as the command
Ctrl+b [ To enter replication mode, pressqexit
Ctrl+b ] Paste the text copied in copy mode
Ctrl+b ~ Lists the prompt message cache

The Window instruction

The prefix instruction describe
Ctrl+b c A new window
Ctrl+b & Close the current window (enter y or n before closing)
Ctrl+b 0 ~ 9 Switches to the specified window
Ctrl+b p Switch to the previous window
Ctrl+b n Switch to the next window
Ctrl+b w Opens a list of Windows for and between Windows
Ctrl+b . Rename the current window
Ctrl+b . Modify the current window number (for window reordering)
Ctrl+b f Quickly locate window (enter keyword to match window name)

Pane instruction

The prefix instruction describe
Ctrl+b The current panel is divided into two parts, and the lower side of the new panel
Ctrl+b % The current panel is divided into two parts, and a new panel is created on the right
Ctrl+b x Close the current panel (enter Y or N to confirm before closing)
Ctrl+b z Maximize the current panel and return to normal after pressing the button again
Ctrl+b ! Move the current panel to a new window to open (valid if there are two or more panels in the original window)
Ctrl+b ; Switch to the panel last used
Ctrl+b q Display panel number. Enter the corresponding number before the number disappears to switch to the corresponding panel
Ctrl+b { Displaces the current panel forward
Ctrl+b } Displaces the current panel backwards
Ctrl+b Ctrl+o Rotate all panels in the current window clockwise
Ctrl+b The direction key Move the cursor to switch panels
Ctrl+b o Select the next panel
Ctrl+b The blank space key Cycle through the built-in panel layout
Ctrl+b Alt + arrow keys Adjust the current panel edge in units of 5 cells
Ctrl+b Ctrl + arrow keys Adjust the edge of the current panel by 1 cell (covered by system shortcuts under Mac)
Ctrl+b t According to the clock

Personalized configuration

The Tmux configuration file is in ~/.tmux.conf. If it does not exist, create it yourself.

Change the prefix shortcut key

If you feel that the default Ctrl+ B prefix shortcut is not working well, you can change it to another shortcut.

Change the prefix shortcut key to Ctrl+ A
set -g prefix C-a
unbind C-b
bind C-a send-prefix
Copy the code

Change the split screen shortcut key

The default split screen shortcuts are not easy to remember, you can change them to look like this.

unbind '"'
bind - splitw -v -c '#{pane_current_path}' # Add panel in vertical direction, enter current directory by default
unbind %
bind | splitw -h -c '#{pane_current_path}' # Add panel in horizontal direction, enter current directory by default
Copy the code

Enabling mouse Support

By default, mouse operation is not supported. If you are not a keyboard controller, you are advised to enable it. After this function is enabled, Pane can be directly selected by mouse and Pane size can be adjusted by dragging.

set-option -g mouse on # Support mouse to select text, etc
Copy the code

Mapping direction key

If you are familiar with Vim, you must like HJKL. Let’s map the arrow keys of Tmux.

# -r stands for repeatable button. For about 500ms, the repeated button will be effective for quick movement
bind -r k select-pane -U # bind k to up
bind -r j select-pane -D # bind j to lower
bind -r h select-pane -L Bind h to the left
bind -r l select-pane -R Bind l to the right
Copy the code

summary

With this treasure tool, uncle’s house is bigger, life is better, life is better and better!