Efficiency: Tmux, the terminal management tool

Source: Internet
Author: User

Efficiency: Tmux, the terminal management tool

This article is from the author woosleyGitChatFor more information, see efficiency King: terminal management tool Tmux. For more information, see the original article.

「 High energy at the end of the article 」

Edit | Habi

Introduction

Tmux is a very practical terminal multiplexing used to manage multiple terminal sessions running in a terminal window. It efficiently and orderly manages all your work terminals through sessions, windows, and panels.

In addition, it can also enable the terminal session to run in the background, access as needed, and share the session with others, which is a powerful tool for remote office and Pair programming. Both front-end and back-end development engineers and O & M personnel are worth adding them to their daily tool list.

Install

In MacOs, it is very easy to install tmux. You can use homebrew to install the latest version:

 

Brew install tmux

For Linux, most releases have tmux packages, which can be installed through the Package Manager. For example, in Ubuntu, apt can be used for installation.

 

Apt install tmux

There are two ways to use tmux in windows:

  • Windows 10 is added to the linux subsystem. You can use Ubuntu linux in windows. The method for installing tmux is the same as that for native Ubuntu;

  • For windows 7 and older versions, Cygwin is recommended. This tool simulates linux in windows and provides installation of most * nix software, including tmux.

Initial use

To use tmux for the first time, you only need to enter the command below the terminal:

 

Tmux

By default, a new session is started ). The window is displayed as follows:

You can see that Tmux has started a new interface on the default terminal. Including the original shell window and the status bar below.

The status bar displays basic information about the current tmux session.

  • [0] indicates the name of the current session;

  • 0: bash indicates the first window of the session on the current day. Its name is bash;

  • The other part is the current host name and the current time.

As a terminal-based tool, all operations of tmux are performed using keyboard shortcuts. After familiarizing ourselves with the tmux shortcut keys, we can completely free our hands from the mouse, it plays a significant role in improving productivity.

The tmux shortcut key uses the Prefix + key form. If you have used Gnu screen, you should be familiar with this mode. The difference is that the default prefix of Gnu screen isctrl + aAnd the default shortcut for tmux isctrl + bPress ctrl and B at the same time. This combination can be customized. In this article, we useprefixTo represent the combination of buttons.

Now we can use the shortcut key to create a new window, pressprefix + c(Press Ctrl + B at the same time, open it, and then press c ).

Shows the effect:

At this time, the entire window does not change much, but the following status bar has an additional1:bash, Indicating that two windows are currently started.

Now you can try to pressprefix + nTo see how tmux switches between different windows.

Servers and sessions

Tmux is essentially a server. When the tmux command is run, a tmux service is run in the background and a session is started. The session communicates with the server through a Unix socket.

By default, the started tmux session is named by a number. For example, the first session is 0. You can use:

 

Tmux new-s session_name

Create a named tmux session, suchtmux new -s test. After a session is started, tmux automatically connects to the session. We can leave the session to place the Tasks running in the session in the background and reconnect as needed.

The session disconnection shortcut isprefix + dAfter the session is detached, the system returns to the previous terminal. In this way, we can easily start a long-term running tmux Session on the remote host and run the program we want to run for a long time. We can usetmux lsView all currently created sessions, for example:

 

Tmux lslearn: 1 windows (created Sun Dec 17 21:03:20 2017) [80x24] test: 1 windows (created Sun Dec 17 20:59:44 2017) [80x24]

Here I have created two tmux sessions named respectivelytestAndlearn.

The name of the reconnect tmux istmux attach. Without any parameters, the latest session is connected by default. You can add parameters.-t $nameThe connection name is$name. For exampletmux a -t learn. At the same time, it shows a tips for tmux. Many tmux commands can be abbreviated. attach is abbreviated as a here.

In the tmux session, you can useprefix + sSelect and quickly switch the tmux session, as shown in. Tmux will pop up a session selection list, you can select the session we want to open through the direction key.

If you want to rename the current session after creating a session, you can useprefix + $In the input box popped up at the bottom, enter the desired name. The command mode of tmux can also be used here. Pressprefix + :In the input box, enter:rename-session new-session(Tab completion is supported) to rename the current session.

Note that after the server is restarted, The tmux session information will be lost. To persistently Save the tmux session information, the tmux plug-in section in this article will introduce a simple and easy-to-use plug-in.tmux-resurrect.

Window

Previously we demonstrated how to create a new window and useprefix + nTo select the next window.

Move between different windows,prefix + nYou can also use the number key to select the nth window. Note that the sequence number of the window in tmux starts from 0.prefix + 1Select the second window.prefix + pTo jump to the previous window.

You can also use shortcutsprefix + wIn the left-side navigation pane.

Useprefix + ,It can be used to rename the current window. The corresponding command mode isrename-window.

Enter exit to exit the current window, but sometimes the window may be stuck.prefix + &, Force the current window to be killed after the input is confirmed.

Panel

The window can be divided into smaller panels, which are used together with the screen display. This makes a hacker feel. First, we use:

 

Tmux new-s pane

Create a new session, and then typeprefix + %And then typeprefix + ", You can get the following split window:

The panel where the current cursor is located is highlighted. We can continue typingprefix + %Andprefix + "View the effect of the continue split panel. To switch between different panels, use the shortcut keyprefix + o. If you want to move up and down to different panels, use the shortcut keyPrefix + direction key.

By default, tmux Splits a panel evenly. We can usePrefix + ctrl + direction keyTo adjust the Panel size.

Sometimes we may need to enlarge a panel to fill the entire window. We can useprefix +!To convert the panel into a new window. Alternatively, you can useprefix + zTo maximize the current Panel as a window and temporarily hide other panels.

The shortcut for forcibly exiting a panel isprefix + x.

Summary

The combination of sessions, windows, and panels is a powerful combination to improve our work efficiency. A recommended method is to create an inaccessible session for different projects, use a window to separate different work content in a project, and then use a panel for screen development. The following screenshot shows the list of all sessions created by my work:

Different projects are stored in different sessions. When I need to switch to a project, useprefix + sJump to the corresponding session, and the Environment configured in the previous project will be restored immediately.

Tmux Configuration

One of the reasons why tmux is more popular than screen is that it is ready for use by default. Of course, we can also customize the tmux configuration through the configuration file. Its default configuration file is~/.tmux.confTo use other files, you can usetmux -fOption to read another configuration file.

Default prefix

We can change the default prefix of tmux through the configuration file. The personal prefix isctrl-zThose who are used to screen can be configuredctrl-a. Here we usectrl-zFor example, use a text editor to open~/.tmux.conf, Add the following content:

 

Set-g prefix C-zunbind-key C-B

Here-gGlobal settings are applied to all sessions we have created. This isctrl-bCan be released and combined.unbind-key C-b.

To make this setting take effect, We Should reload. tmux. conf. Typeprefix + :Open the naming mode and enter it in the input box.source ~/.tmux.conf. Now we can use the new prefix.

Quickly reload the configuration file

It is too difficult to use the command line mode to reload the configuration file after modification. We can customize a shortcut to simplify this operation. In~/.tmux.confAdd:

 

Bind-key r source-file ~ /. Tmux. conf \; display-message "Config reloaded"

The last time we usedprefix + :+source ~/.tmux.confTo reload the configuration file. Then you can useprefix + rTo do this. Add it to. tmux. conf.

 

Bind e new-window-n ". tmux. conf" "vim ~ /. Tmux. conf"

Thisprefix + eYou can quickly open. tmux. conf and modify the configuration.prefix + rThe operation can be completed in seconds.

Change default number

Since the default sequence numbers of the window and Panel of tmux start from 0, we can change this setting so that the default sequence number starts from 1.

 

Set-g base-index 1 setw-g pane-base-index 1

Change the shortcut key of the split panel

The shortcut keys for the vertical and horizontal split panels of tmux areprefix + %Andprefix + ". These two buttons are hard to remember. We can change themprefix + |Andprefix + -. Add the following configurations~/.tmux.conf.

 

Bind-key | split-window-h bind-key-split-window

Change the shortcut key for moving between panels

Most keyboard tools, such as vim, usejkhlTo move up, down, and left. We can bind the move operation between panels to the corresponding buttons. The configuration is as follows:

 

Bind-key l select-pane-Rbind-key h select-pane-Lbind-key j select-pane-Dbind-key k select-pane-U

Panel size adjustment

We can adjust the Panel size in a way similar to the move key. Redefine the shortcut key as JKHL. The configuration is as follows:

 

Bind-key L resize-pane-R 5bind-key H resize-pane-L 5bind-key K resize-pane-U 5bind-key J resize-pane-D 5

Mouse Mode

The mouse mode may also be very useful. For example, you may want to use the mouse to select a panel or window, adjust the Panel size with the mouse, or scroll up to view the history.

To enable or disable the mouse modeset -g mouse on/offIn command mode. We can go further by binding to the shortcut keyprefix +mTo trigger the mouse mode switch. The configuration is as follows:

 

Bind m run 'old = $ (tmux show-gv mouse); new = ""; if ["$ old" = "on"]; then new = "off "; else new = "on"; fi; tmux set-g mouse $ new; tmux display "mouse: $ new "'

Note that this is a line of configuration, which may have been discovered by careful readers.shellScript, open it in mouse mode, and vice versa.

Status Bar Configuration

By default, the status bar of tmux is a green bar below the window, such:

It consists of three parts:

  • The rightmost session name;

  • Window list in the middle;

  • Host information on the left.

The displayed information, as well as the font foreground color and background color can be flexibly configured. The advantage of open source is that the Community already has many mature solutions, and powerline is the most popular among them.

Powerline is written in Python and installed using the Python package management toolpip

 

Pip install powerline-status

In addition, we also need to install the font used by powerline, which can be downloaded and installed in the https://github.com/powerline/fonts.

To use powerline for tmux, you only need to add the following configuration to. tmux. conf,prefix + rReload:

 

Source "{repository_root}/powerline/bindings/tmux/powerline. conf"

Repository_root is the installation path of powerline. The following is a screenshot of the status bar after the author installs powerline:

Replication Mode

A common problem for beginners of tmux is that they cannot use the scroll key to scroll up to view the terminal history. To do this, you must enter the replication mode of tmux.

Pressprefix + [, Tmux enters the replication mode, and then press the Enter key to exit. By default, tmux saves 1000 lines of terminal history. We can pass.

 

Set-g history-limit 10000

Adjust the size of historical records.

The tmux replication mode can use two keyboard modes: Vi and Emacs. The default mode is Emacs:

 

Set-g mode-keys vi

Change to the Vi keyboard mode. In Vi mode, you can use jkhl to move the cursor up or down.

To copy the text, first enter the copy mode, move the cursor to the specified position, press the Space key, and then move the cursor to select the text. The selected text is highlighted, and then press the Enter key, indicates that the selection is complete.

Enter the commandtmux list-buffer, We can see the text saved in the tmux cache, usingtmux paste-bufferOr shortcut keyprefix + ]You can paste the content in the cache area.

More advanced is that tmux maintains a buffer stack. Every time it is copied, tmux creates a new buffer at the top of the stack. Now we can copy the text several times, and then type the commandtmux list-buffersYou can see a buffer list.

Run the command again.tmux choose-buffer, Tmux creates a selection box. We can select the text to be pasted and press enter to insert the text to the cursor position.

Vim users can reconfigure these commands and shortcut keys to become more familiar with the use.

 

Bind-key-Tcopy-mode-vi 'V' send-X begin-selectionbind-key-Tcopy-mode-vi 'y' send-X copy-selectionunbind pbind p paste-bufferbind B choose-buffer

In copy mode, you can use v and y to select text.prefix + pPaste text, useprefix + bTo select the buffer (users who set the prefix to ctrl + B can select another shortcut ).

Other configurations

In order to make tmux work better, I have posted some other basic tmux configurations here.

 

Set-g default-terminal "screen-256color" # Color support setw-q-g utf8 on # utf8 support set-q-g status-utf8 on # tmux <2.2 setw-g automatic-rename on # automatically rename the window set-g renumber-windows on # re-calculate the window indexset-g display-time 4000 when closing the window # The message prompt time of tmux is 4 seconds.

Session sharing and Pair Programming

At present, many enterprises are implementing Pair programming, and two people develop software before the same workbench. When developers are in the same office, implementing Pair programming is relatively simple, but if developers are in a remote state, implementing Pair programming requires screen-sharing software.

The client-based session sharing of tmux can provide a good pair programming experience even if the network conditions are poor.

Let's first look at the most basic session sharing mechanism.

Suppose there is A host Foo. programmer A ssh connects to this host and uses tmux to start session pairing.

 

Tmux new-s pairing

A wants to share the session with developer B, so he only needs B to log on to the same user of the same host, and B can use the command.

 

Tmux attach-t pairing

Attach to the same session. At this time, A and B will see A unified window for the same session, and their operations will be fully synchronized to their respective screens.

This method is not so perfect because the screens A and B will always see are exactly the same. Sometimes we may need to make A and B do different things at the same time, synchronize the results of the window without the need to synchronize the results displayed in the window. To do this, you only need B to use the command:

 

Tmux new-s test-t pairing

Create a new session and add it to the pairing session. In this way, A and B See the same window result, but both of them can be input independently without disturbing each other.

If you want to share the tmux session without sharing the login permissions of the server, try tmate. It is a fork of tmux and can share your tmux sessions read-only without having to log on to the host. For more information, see related websites.

Plugin Management

Tmux officially supports a series of plug-ins that can be found in https://github.com/tmux-plugins. Note that most plug-ins require the tmux 1.9 + version.

Tpm

The first thing to mention is the plug-in management tool tpm, which can be used to conveniently install and delete plug-ins. To install tpm, follow these steps:

 

Git clone https://github.com/tmux-plugins/tpm ~ /. Tmux/plugins/tpm

In~/.tmux.confAdd these configurations:

 

# Tmux plug-in list set-g @ plugin 'tmux-plugins/tpm 'set-g @ plugin 'tmux-plugins/tmux-sensible' # insert this line. tmux. run '~ /. Tmux/plugins/tpm'

Then useprefix + rReload the tmux configuration to use tmp.

When a new plug-in is added to. tmux. conf, we can useprefix + IInstall the plug-in and useprefix + UUpdate the plug-in. The shortcut for deleting a plug-in isprefix + alt + u.

Session Persistence: tmux-resurrect

Tmux-resurrect is a lightweight session persistence plug-in. It can be used to reload the previously saved tmux session after the server is restarted.

Install tmux-resurrect.set -g @plugin 'tmux-plugins/tmux-resurrect'Add to. tmux. conf, and then pressprefix + IInstall.

The method for saving the tmux session isprefix + C-s. The following prompt is displayed after the file is saved successfully.

If you restart the machine and start a new tmux session, you can use the shortcut key.prefix + C-rResume the saved session information.

Tmuxinator is another tmux session persistence tool that maintains sessions by editing and reading configuration files. This is for your reference.

Conclusion

So far, this article's introduction to tmux is over. As mentioned at the beginning of this article, tmux can greatly improve work efficiency and hope that readers will love this excellent terminal management tool.

Websites mentioned in this Article

Homebrew: https://brew.sh/

Cygwin: https://www.cygwin.com/
Powerline: https://github.com/powerline/powerline
Https://tmate.io/tmate/
Tpm: https://github.com/tmux-plugins/tpm
Tmux-resurrect: https://github.com/tmux-plugins/tmux-resurrect
Tmuxinator: https://github.com/tmuxinator

Recent hot news

About source code leakage-WEB Security

What are the skills required to write C # in writing with LINQ?

Machine Learning INTERVIEW TIPS

In-depth introduction to JS asynchronous processing technical solution

Agile coach V-shaped six-step practice: From the Brown Movement to in-depth collaboration

Building an AI speaker Alexa voice service from scratch

Modify order amount !? 0.01 yuan to buy iPhone X? | Web Logic Vulnerabilities

「 Read original 」Here is all you want to know about the exchange record.

Copyright Disclaimer: This article is an original article by GitChat author and cannot be reproduced without GitChat's permission.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.