Because of the recent need to SSH to a remote environment a lot, I took advantage of this to do some tMUX. After all, to do a good job, you must first sharpen your tools

The following is the address of my configuration file, which is still being explored and updated. I liked the Solarized theme so much that I referenced it for the status bar theme. I will list some of the techniques I use in my daily life in the following sections, and feel free to add them. Please click a like or star if it can help you

  • Github.com/shfshanyue/…

Here’s a screenshot for a sneak peek, from the Mac.

Shanyue. tech/ POST /tmux-s…

Introduction to the

tmux is a terminal multiplexer

Tmux is a terminal multiplexer that allows you to manage multiple terminals on one screen!

As I understand it, TMUX has three major benefits

  1. Split screen of course,iterm2You can do split screen, butiterm2There is one drawbackiterm for Mac. Moreover,iterm2The split screen also needs to be constantlyssh <server>. The result is that there are multiple users connected to useps aux | grep sshd | grep -v grepCheck the number of connections.
  2. attach attachCan play a role in protecting the scene, not due tossh timeoutAnd lose the working environment.
  3. Can be configured to change

basis

Start the

Create a new TMUX session, but you are advised to use the following Tips 1 to do so

$ tmux      # Create a TMUX session, but you are advised to use Tips 1 below.
Copy the code

The term

  • Server contains multiplesession
  • Session contains more than onewindow
  • windowSimilar to theiterm2Tab, contains multiplepane, the following Chinese will be translated as Windows
  • paneSimilar to theiterm2Pane, the following Chinese will be translated as panel

Common command

  • bind-key (bind): Bind the shortcut key and pressprefixKey and shortcut key trigger.
  • Set-option (set) : sets options
  • Source-file (source) : the current configuration file takes effect
  • new-window: Creates a window, default shortcut keyprefix c
  • The split – window: split screen

Custom status bar

I like the Solarized theme, but the background color is not beautiful in the terminal, so I chose a background color in Tomorrow instead.

message-command-style

Set the style of the status bar information, separated by commas, set the foreground color FG, background color BG, and text decoration, such as italics, bold, and so on. The following example

"fg=yellow, bg=#abcdef, underscore"
Copy the code

Custom status bar

You can customize the content of the status bar, such as window number, window name, host, power consumption, time and so on.

  • #{host} tmuxIt provides some variables about its state, for examplesession-name.window-name.hostAnd so on. Such as to#{host}I’m going to use this form insteadhost.#{window-name}Instead ofwindow-name.
  • #(uptime)Except in the status bartmuxRelated information, sometimes also need to obtain system related information, such as startup time, current user. These system commands can be expressed as#(uptime).
set -g message-style "bg=#00346e, fg=#ffffd7"        # tomorrow night blue, base3

set -g status-style "bg=#00346e, fg=#ffffd7"   # tomorrow night blue, base3
set -g status-left "# [bg = # 0087 ff] ❐ # S"       # blue
set -g status-left-length 400
set -g status-right "# {? client_prefix, ~ , } #[bg=#0087ff] #h #[bg=red] %Y-%m-%d %H:%M "
set -g status-right-length 600

set -wg window-status-format " #I #W "
set -wg window-status-current-format " #I #W "
set -wg window-status-separator ""
set -wg window-status-current-style "bg=red" # red
set -wg window-status-last-style "fg=red"
Copy the code

Tmux Tricks

keepsshThe connection

Every time a new session is created, bring the name for the convenience of attaching next time. Just to mention the detach command, the default prefix D shortcut will detach the current session first.

$ tmux new -s shanyue
$ tmux detach
$ tmux attach -t shanyue
Copy the code

Fast-moving panel

The move panel command is select-pane, which can be configured as a Vim-style move command.

bind -r h select-pane -L 
bind -r l select-pane -R
bind -r j select-pane -D
bind -r k select-pane -U
Copy the code

The -r parameter indicates the repeatable button. For example, prefix r r indicates prefix r, prefix r. The keystroke time needs to be set through repeat-time, which is generally 500ms.

You can also enable mouse support to move the panel quickly.

Send to terminalprefix key

I used Crtl-s as the prefix key and send-prefix as the command to send the prefix key to the terminal.

set -g prefix2 C-s                                                             
bind C-s send-prefix -2
Copy the code

Rename the window name

Rename -window command to rename a window. The default shortcut key is prefix.

But there is a small problem, every time after renaming the window name, typing a few Spaces will automatically rename, their hard work was destroyed…

You need to configure the following two configurations to turn them off and finally rename them

set -wg allow-rename off                                                            
set -wg automatic-rename off
Copy the code

Configuration edit and restart shortcut keys

Setting open and restart configuration files as shortcut keys can quickly improve configuration efficiency. Set prefix r to restart and update the configuration to facilitate debugging of the configuration file. Prefix e Opens the configuration file.

Source ~/.tmux.conf Restarts the configuration file to take effect. New-window -n

command Creates a window and sets the window name. -n indicates the name of the newly created window

bind r source ~/.tmux.conf\; display "tmux config sourced"
bind e neww -n tmux-config "\${EDITOR:-vim} ~/.tmux.conf
Copy the code

Enabling mouse Support

$ tmux set -g mouse on

Mouse support is disabled by default. After the mouse is enabled, you can copy, flip screen, switch panel, switch window, and resize.

The mouse support is powerful, and so far an Iterm2 has been successfully built. But I don’t like the mouse mode, so it’s banned.

Besides, after opening the mouse support, who can operate my terminal, a bit forced case also did not have

Keep current path

When pane and Window are new, keep the current path. Add the -c parameter to the previous command to indicate the path to a new window or panel.

The command to open a new panel is split-window

bind c new-window -c "#{pane_current_path}"
bind % split-window -h -c "#{pane_current_path}"
bind '"' split-window -c "#{pane_current_path}"
Copy the code

Maximize the current panel

The command is tmux resize-pane-z, and the default shortcut is prefix Z. To view more detailed information, press Prefix Z to enter the full screen. After that, press Resume again. Pretty cool feature.

Turn over the screen

The first time you use TMUx, with Webpack, the output is a lot and useful error messages are overwritten. At this point, scrolling up is very important. Now let’s talk about the two modes in TMUx Window,

  • default-mode

    It’s just entered the tMUX default mode.

  • copy-mode:

    Press prefix to enter this mode, which is similar to normal mode vi(Emacs) and supports copy, paste, search, and page turning. The default mode is vi. If not, set it to vi. If not, set it to vi

    $ tmux show-window-options -g mode-keys
    Copy the code

    Same as vi, you can use C-D and C-u for page up and down (half screen), but you can also use HJKL.

Alternatively, you can turn on mouse support and use the scroll wheel to scroll through the screen.

Copy and paste

So copy-mode, copy and paste. After entering copy-mode, v will be selected and Y will copy and exit copy-mode. Use prefix] to paste.

V & Y is a user-defined configuration. The configuration is as follows

bind -t vi-copy v begin-selection
bind -t vi-copy y copy-selection
Copy the code

The copy operation stores the contents into buffer. You can become more familiar with the following commands to manipulate buffer

$ tmux list-buffers          # list all
$ tmux show-buffer -b [name] The latest buffer is displayed$ tmux choose-buffer&emsp; &emsp; &emsp;Enter the buffer selection screen, more flexible
Copy the code

Alternatively, you can turn on mouse support and use the mouse to select text.

Find keywords

Now that you are in copy-mode, those familiar with vi must know that the search is/and? .

Quick location window

Suppose you create multiple Windows and need to locate a window quickly. You know what’s in that window, but you can’t remember the window number.

A good solution is the find-window command, and even better is the default prefix F shortcut. Enter the keyword of the window content, you can quickly locate the window, but there is a small disadvantage, it is unable to locate the panel!

Last but not least

Man tmux! ** do not see the document is not skilled, do not see the source code is not proficient. ** As a result, you need to read more documents and commands.

extension

  • practical-tmux
  • gpakosz/.tmux (stars 1569)
  • Tmux-resurrect protects and restores to working state