What is Tmux?

Tmux: Terminal MultipleXer So the question is, what does Tmux provide to enable terminal reuse?

  • Terminal Session Management
  • Terminal Window Management
  • Tmux window Panel

Why Tmux?

  • It is service-centered and applies to services such as remote SSH
    • Preventing Terminal Crashes
    • Remote pairing
  • Multi-terminal, multi-window support
  • Excellent collaboration with VIm

Install Tmux

# linux
sudo apt-get install tmux

# macOS
brew install tmux

# use window: https://codeandkeep.com/Tmux-on-Windows/

Install Tmux using mysys64 pacman and Git Bash.
Copy the code

Tmux three-tier mental model

  • Session layer: Services are the foundation
  • Window layer: the concept of running in Session
  • Pane layer: The concept of running on a window

Tmux help command

ctrl-b, ? ...## Common shortcut key prefixTmux contains two sets of ways to operate tMUx window, Panel, one is TMUx command, the other is tMUx command corresponding shortcut key. Use 'tmux list-keys' to print all the shortcut keys for the machine. - 'CTRL + B' tmux can be accessed from the TMUx operation terminal either by using the Tmux command, or by 'CTRL + B, <other>'.### Change the prefix

```sh
vim ~/.tmux.conf Edit or create a configuration file

set -g prefix C-a Set the global prefix to Ctrl + A
unbind C-b Unbind the original Ctrl + B
Copy the code

Terminal session

The interaction between the user and the computer through the terminal is called a “session”, but the session ends when the terminal is closed. Sometimes, you don’t want it to end, you just want to cut to a new window to do something else. To meet these needs, terminal multiplexers such as Tmux can implement multi-background session functionality.

tmux info Get the current session information
Copy the code

Tmux sessions – Add/chain/cut/delete/change/search

Added: Default session

tmux Add a default session
Copy the code

Added: Named session

Run the new command and the -s parameter to create a new one

# new-s cooperate
tmux new -s <name>

# new-session-t
tmux new-session -t <name>
Copy the code

Link: Existing session

  • According to the number
  • According to the name
tmux attach -t 0
tmux attach -t <name>
Copy the code
  • Links to the most recently used session
tmux a
Copy the code

Switch: An existing session exists

  • According to the number
  • According to the name
tmux switch -t 0
tmux switch -t <name>
Copy the code

Exit (delete) : session

  • Use shortcut keys:ctrl + d
  • Use the following command:exit

Kill: session

Delete by session id:

tmux kill-session -t 0 
Copy the code

Delete by session name:

tmux kill-session -t <name>
Copy the code

Killing the current session

  • ctrl + b, x

Split: session

Separation can be simply understood as background mode:

  • Use shortcut keys:ctrl + b, d
  • Use the following command:tmux detach

Changed: Existing session

  • Tmux command
  • Shortcut:ctrl+b, $
tmux rename-session -t 0 <name>
Copy the code

View: Session

  • Tmux command
  • Use shortcut keys
tmux ls

tmux list-session
Copy the code
  • ctrl + b, s(A visual window)

rename

  • ctrl + b, $

The window management

Create a window

tmux new-window

tmux new-window -n <window-name>
Copy the code
  • ctrl + b, cCreate a new window

Bind new shortcut keys:

unbind n  # remove the n binding
unbind w  # remove the w binding

ind n command-prompt "rename-window '%%'" # n Rename window
bind w new-window -c "#{pane_current_path}" # w Create a windW
Copy the code

use

The switching window

tmux select-window -t <window-number/window-name>
Copy the code
  • Switch (up) :CTRL + b, p
  • Switch (down) :CTRL + b, n
  • Toggle (specify number) :CTRL + b, <number>

Rename the window

tmux rename-window <new-name>
Copy the code
  • Rename:CTRL + b,,

Choose window

  • The list window:ctrl + b, w
  • A list of sessions:ctrl + b, s
  • List pane:ctrl + b, p

Window of the same session

Create multiple Panes within a session. If you run the create Window command under a window, it belongs to the same session.

A front-end project department is managed by three Windows under one session:

  • A service window
  • A Git commits the window
  • A Vim editor

The Panel management

Create a Panel

  • The level of the Panel
  • The vertical Panel

Tmux command

tmux split-window
tmux split-window -h
Copy the code

Shortcut key of the tmux command

  • tmux split-windowIs equivalent toctrl + b, ";
  • tmux split-window -hIs equivalent toctrl + b, %;

Bind a new shortcut key

unbind v  # unbundling v
unbind h # unbundling h
unbind %  # % unbundling
unbind '"' # unbundling"

bind v split-window -h -c "#{pane_current_path}" # bind v: Creates a vertical pane for the current window
bind h split-window -v -c "#{pane_current_path}" Bind H: Creates the horizontal pane for the current window
Copy the code

Close (delete) Panel

  • useexitexit
  • Shortcut:Ctrl + xClose the current pane

Mobile: select – pane

The mode of tmux command:

tmux select-pane -U (Upper)

tmux select-pane -D (Down)

tmux select-pane -L (Left)

tmux select-pane -R (Right)
Copy the code

CTRL + B,

Shortcut:

  • Switch by direction:Ctrl + b, arrow
  • Switch to the previous Panel:Ctrl + b, ;;
  • Switch to the next Panel:Ctrl + b, o;

The Tmux configuration file is bound with Vim mode shortcut keys

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
Copy the code

Integral movement:

  • Overall forward:ctrl + b, ctrl + o;
  • Overall move back:ctrl + b, alt.+ o;

Exchange swap – pane

The mode of tmux command:

tmux swap-pane -U (Upper)
tmux swap-pane -D (Down)
Copy the code

CTRL + B,

Shortcut:

  • To exchange with:Ctrl + b, {
  • With small exchange:Ctrl + b, }

independent

  • Separate panel from window:ctrl + b,!

Full screen/Exit panel

  • ctrl + b, z

Adjust the

  • ctrl + b, ctrl + <arrow key>
  • Mouse mode is enabled to use the mouse

Serial number information

  • ctrl + b, q

All TMUX commands

tmux list-commands
Copy the code

Tmux mode

Tmux, like Vim, has the concept of patterns, and Tmux defaults to default mode.

The default mode

The default is vim-equivalent insertion mode, which allows you to type whatever panes you want in different text boxes

Copying model

Configure the replication mode using VIm

set-window-option -g mode-keys vi
Copy the code

Use tMUx’s copy mode as with VIM

The configuration file

Check out more history

set -g history-limit 100000
Copy the code

The path

vim ~/.tmux.conf
Copy the code

overloading

tmux source-file ~/.tmux.conf
Copy the code

If you need to reload Tmux after modifying the configuration file, you can set the following shortcuts:

unbind r # unbind r
bind r source-file ~/.tmux.conf \; display "Reloaded ~/.tmux.conf" # Rebind r key function
Copy the code

Rebind shortcut

unbind C-b # unbundling
set -g prefix C-a # Set the prefix shortcut key globally
Copy the code

Tmux workflow

Tmux generally works with Vim to perform terminal operations without a mouse.

Project-based workflows

  • The project name is session-name
  • The project’s Git commit window
  • The project’s Server service Window
  • The project’s Edit edits the Window
    • Edit window to create panes as needed
cd your-project-dir

tmux new-session -t your-project-name
tmux new-window -n git # for submission
tmux new-window -n server Start the service
tmux new-window -n edit # Cooperate with Vim editing
Copy the code

Under the project folder, we create a simple Tmux session to start the service. Essentially, we use the workflow of switching Windows mode, and the core work does not affect each other.

Support mouse

Although it is recommended that terminals do not have a mouse, Tmux also supports mouse mode. The main mouse mode is:

  • The selected
    • window
    • Pane, resize
  • copy
    • Operations such as copying text
set -g mouse on # Enable the tmux Mouse function
Copy the code

Tmux management tools

Tmux Session management tool

  • Tmuxp (based on Python)

Tmux plug-in management

  • TPM Tmux plug-in management tool

Tmux plug-in

  • Tmux-resurrect permanently saves the session plug-in

Tmuxn A Tmux session management tool

Tmux is already very convenient for managing sessions, but we wanted to have a more convenient way of managing them – configuration, configuring a session to manage us according to the project.

Here’s a scenario: If you’re working on three React Web front-end projects by yourself, you might have to open three projects after each shutdown, start services before and after, and do the same thing every day. Tmuxn is the ability to start projects from session. The essence is simple

  • configuration
  • Command start service
npm -g i tmuxn

tmuxn -h
Copy the code

Tmuxn provides the following commands

  • Tmuxn uses the project name command:
tmuxn -c/--create your-project-name Create a project with the project name
tmuxn -s/--start your-project-name Use the project name to start the project that has been created
tmuxn -k/--kill your-project-name Kill an item with the item name
tmuxn -d/--debug your-project-name # Use debugging
Copy the code
  • Tmuxn Default configuration file location:/Users/magnesium/.config/tmuxn
Created project file: /Users/xxx/.config/tmuxn/test.yml
Copy the code
  • Use the existing TMUxN configuration file
tmuxn -p/--project <project_config>
Copy the code

Tmux configuration file

name:test
root: ~ /

# new Windows
window:
    - editor: # the window name
        - pane: The # Editor window contains two panes
            - vim # start vim
            - mongo # start server
    - server: yarn start:dev  # start service
    - git # for git bars
Copy the code

Tmuxp management session/window pane

Tmuxp is a TMUX session management tool developed in Python and uses YAML to write configuration files

A simple TMUXP configuration file

session_name: your_sesssion_name
start_directory: your_project_path
windows:
-window_name: server
    panes:
        -shell_command:
            - yarn start
            - code your_project_path
-window_name:git
Copy the code

Configuration file analysis:

  • Session_name: tMUXP tMUX session name to be created
  • Start_directory: indicates the path where the session is started
  • Windows: indicates information such as the window name in the current session
  • Panes: indicates the work of all terminals in the current window. Such as:
    • Start a front-end react service
    • You need to start vscode
    • other

Tmuxp command line

  • tmuxp loadRead the yamL configuration file,

reference

  1. Tmux source library
  2. Tmux tutorial
  3. What’s tmux and How to Configure It: a Detailed Guide
  4. Tmuxp document