I remember it was a year ago when I first got in touch with TMUx. At that time, I didn’t want to open multiple Item2 tabs in order to reuse terminals, but later I found that I couldn’t use it at all, so I kept lying there until I picked it up again half a year ago and began to learn some tMUx usage. Now half a year has passed, I really understand why TMUx is a magic tool.

I’ve been using iterm2 before TMUx, with its native new TAB and split screen, but it’s fine, the interface looks something like this:

However, after a lot of split screen, you can see a lot of status bar, which is not very beautiful. Compared with TMUX’s split screen, it looks much hack. Tmux’s split screen interface is as follows:

It is for this reason that I started learning to use TMUX.

The installation

Tmux’s warehouse: github.com/tmux/tmux

Install tMUX first:

# MAC installation
brew install tmux

# Linux installation
sudo apt-get install tmux
Copy the code

Before you start learning TMUX, you should first clarify a few concepts:

  • Session: Encapsulates a task. It is a collection of Windows. A session is created before the task starts, and the status related to the task is stored in the session. One of the most useful aspects of a session is the persistence of working state
  • In Chrome, opening a new TAB creates a new page, and tMUx is similar
  • Pane: A single window can be divided into panes, just as the iterm2 split screen was used earlier

Simply put: Session contains window, window contains Pane

Session

new

After the installation is complete, enter TMUx on the terminal to access tMUX. The session name is 0. This is because tMUx creates a nameless session. If you want to create a session name, you can use the following command:

tmux new -s your-session-name
Copy the code

Disconnect the

You can use this if you want to disconnect the current session

tmux detach # Disconnect the current session
Copy the code

Alternatively, use the shortcut [prefix]+ D, where Prefx defaults to CTRL + B

restore

After exiting the session, to restore the previous working area, enter:

tmux attach-session -t your-session-name

# can be abbreviated
tmux a -t your-session-name
Copy the code

Session-name is optional. If this parameter is not specified, the first session is entered by default.

Shut down

To disable the tmux kill command, run the following kill command:

  • kill-server: Close sessions, clients, and server. This command closes all tMUx creations
  • kill-session
  • kill-window
  • kill-pane
# to close the session
tmux kill-session -t your-session-name
# close all
tmux kill-server
Copy the code

To view

tmux list-session
# shorthand
tmux ls
Copy the code

Or use the shortcut key prefix+ S to view all sessions and prefix+ W to view all Windows.

Basic TMUX configuration

Tmux configuration is in the ~/.tmux.conf file. If the file does not exist, create a configuration file first.

Configure the TMUx prefix

The default prefix is CTRL + B. Press prefix to activate TMUx, and then press other keys to perform TMUx operations. One advantage of this approach is that it can effectively avoid conflicts with other application shortcuts.

But if you use TMUx a lot, you will find that CTRL and B are not very comfortable to press. Can we change a more comfortable prefix key? Of course it is possible, just unbind the original prefix in the TMUX configuration file and modify it to our own custom. The configuration is as follows:

set -g prefix C-a

Unbind the original prefix
unbind C-b
bind C-a send-prefix
Copy the code

After the configuration is complete, restart tMUx or press CTRL + B and enter: to enter command mode, enter:

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

Or we can add shortcuts to make the configuration work:

bind r source-file $HOME/.tmux.conf \; display-message 'Config reloaded'
Copy the code

In this way, just type prefix+r for the configuration to take effect.

Split screen

In TMUX, the default shortcut keys for horizontal and vertical split screen are prefix+” and prefix+%. These two keys are rarely used and not easy to press. We can customize the shortcut keys we are accustomed to.

bind C-h splitw -v -c '#{pane_current_path}'
bind C-v splitw -h -c '#{pane_current_path}'
Copy the code

Splitw stands for split-window, and -c ‘#{pane_current_PATH}’ means that when a new pane is opened, the current directory is entered by default.

Switch pane

In Iterm2, the shortcut for switching labels is Commond + Option + or Commond +[or Commond +]. In TMUx, the default shortcut for switching pane is Prefix +, but the arrow keys themselves are too far away from CTRL to be convenient. Similarly, we can set our favorite shortcut keys, for example, set the same shortcut keys as VIm (here to say, in fact, the most commonly used control key in VIM is HJKL, if you are used to vim, you can set the same shortcut keys as TMUx).

bind h select-pane -L # up
bind j select-pane -D # down
bind k select-pane -U # left
bind l select-pane -R # right
Copy the code

Enabling mouse Support

When I first started using TMUx, I always found various commands troublesome and difficult to remember. It would be better if I supported the mouse. In fact, to support the mouse, I only need to enable it in the configuration file:

set-option -g mouse on
Copy the code

After the mouse is turned on, the pane switch can be activated by clicking directly, and the split-screen size can be adjusted by dragging and dropping directly.

Of course, since the use of TMUx, the purpose is not just to look good, but to improve efficiency, so try to use less mouse 😏

Adjust the pane

If you want to use shortcuts to resize the Pane, you can also set shortcuts:

bind H resize-pane -L 10 Expand to the left by 10
bind L resize-pane -R 10 # Expand right by 10
bind K resize-pane -U 5  # Expand up 5
bind J resize-pane -D 5  # Expand down 5
Copy the code

This extends Pane through Prefix +HJKL

The attached

system

The prefix instruction describe
Ctrl+b ? Displays the shortcut key help document
Ctrl+b d Disconnect the current session
Ctrl+b D Select the session to disconnect
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 : To enter the cli mode, run the ls command
Ctrl+b [ To enter replication mode, press Q to exit
Ctrl+b ] Paste the text copied in copy mode
Ctrl+b ~ Lists the prompt message cache

Window

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

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, return to normal after pressing the button again (added in V1.8)
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

reference

  • Tmux User manual
  • ITerm2 shortcut keys
  • tmux shortcuts & cheatsheet
  • tmux cheat sheet