Tmux Crash Tutorials: Tips and tweaks

Source: Internet
Author: User

This article turns from: http://blog.jobbole.com/87584/introduction

Some developers often use the terminal console to work, resulting in the eventual opening of too many tabs. If you are one of them, or you are practicing pairing programming, then I recommend you read this article. Starting last month, I started to use Tmux extensively and found Tmux very practical, so I think I should write an article to share with you some suggestions and professional solutions for using TMUX. This article will first describe what Tmux is and then explain how to use Tmux to make it work with Vim to create a more efficient and elegant terminal tool.

This article will contain the following content:

    • The foundation of Tmux
    • The best features in Tmux
      • Window (Windows)
      • Panes (Pane)
      • Sessions (session)
      • Quickly move the cursor or copy text between text
      • Very lightweight pair-pair programming capability
    • Adjust the Tmux to enhance its integration with Vim
      • Adjust the color scheme of the background
      • Adjust the shape of the cursor
      • Adjust text indent when pasting
    • Other tools or techniques to enhance the TMUX experience
      • Automatically create a session with Tmuxinator
      • Change the color of the Tmux status bar

Note that during this writing, I installed the following set of software and used them during testing:

    • Tmux 1.9a
    • Vim 7.4
    • ITerm 2.1
    • Mac OS (Mavericks and Yosemite)

Let's get started!

Basic knowledge what is Tmux?

Tmux is a tool for running multiple terminal sessions in a terminal window. Not only that, you can also use Tmux to make the terminal session running in the background or on-demand access, break the session, this function is very practical. Later, we'll see how to make the most of this feature.

, this is a Tmux session:

We can see:

    • Left: Vim
    • Right: System Shell
    • Lower left: The name of the Tmux session ("Pomodoro-app")
    • Lower middle: Tmux window in current session ("app log", "editor" and "Shell")
    • Bottom right: current date
How do I install Tmux? Install in Mac OS:
    • Installing Homebrew
       
      Ruby-e "$ (curl-fssl https://raw.githubusercontent.com/Homebrew/install/master/install)"

For more information on installing homebrew, refer to here.

    • Installing Tmux
      $ Brew Install Tmux
Install in Ubuntu:

Enter the following command at the terminal:

sudo apt-get install Tmux
 Tmux shortcut key prefix (Prefix)

In order to make its own shortcut keys and other software shortcut keys do not interfere with each other, Tmux provides a shortcut key prefix. When you want to use a shortcut key, you need to press the shortcut key prefix before pressing the shortcut key. The shortcut key prefix used by the TMUX is the key combination Ctrl-b (press the key and the key at the same time Ctrl b ). For example, if you want to list the sessions in the current Tmux with shortcut keys (the corresponding shortcut is s), then you only need to do the following steps:

    • Press key combination Ctrl-b (Tmux shortcut prefix)
    • Release the combo keyCtrl-b
    • Press the s key

Here are some tips:

First, I recommend swapping Ctrl keys and Caps-Lock keys.

It is useful to press the Caps-Lock key instead of Ctrl the key. Because in the coding process, you need to press the Ctrl key frequently, and because the Caps-Lock finger in the beginning of the keyboard in the same line, so press the Caps-Lock key will be easier and more convenient.

Second, I recommend that you change the Tmux shortcut key prefix to Ctrl - a . After replacing the key with the key Caps-Lock Ctrl , because the key and the key is closer, so the press will be easier Caps-Lock a than the Ctrl - a press Ctrl - b , more convenient.

To change the shortcut key prefix to Ctrl-a , add the following configuration to the Tmux configuration file ~/.tmux.conf :

Unbind c-bset-g prefix c-a
Configuration file for Tmux

Each time a new session is opened, Tmux will read ~/.tmux.conf the file first. This file contains the configuration of the Tmux.

Tip: If you want the new configuration item to take effect immediately, you can add the following line of configuration to the file ~/.tmux.conf .

# bind a reload keybind R source-file ~/.tmux.conf; Display-message "Config Reloaded ..."

Once this is configured, each time you ~/.tmux.conf add a new configuration to the file, you can simply press to reload the Ctrl-b r configuration and make the new configuration effective, eliminating the need to open a new session.

The best features in Tmux

Tip: The following may be slightly different from the interface you see when you use Tmux. This is because I modified the Tmux status bar configuration, if you want to modify the same as the effect, then you can refer to the "beautify the Tmux status bar" section of the steps.

window pane

I think it's a good idea to split the screen vertically, so that I can use Vim on one side and see the result of the code running on the other side, and sometimes I open another console if I need to. Below I will explain how to use Tmux to achieve all this.

Can be seen:

    • Left: Vim (the top left is a Ruby class file, the bottom left is a test file written for this type)
    • Right: a Bash session

To create a vertically placed pane it's easy to open a Tmux session and then just press Ctrl-b % it and a vertical pane appears. In addition, to split the screen horizontally, you only need to press Ctrl-b " . Moving the cursor between Tmux panes is also very simple, you just need to press the Tmux shortcut key prefix, and then press the corresponding arrow key to let the cursor into the target pane.

Window

In Tmux, a window is a pane container, and you can place multiple panes in a window and arrange multiple panes in a window depending on your actual needs, and it's entirely up to you. For example, I often do this by opening a window called "Server" for the server running the application (where you can see the log of the server) and then opening another window called "editor" for writing code. There are two panes in this window, one for Vim and one for running the test code. Finally, a window called "Shell" is opened for running commands through the Bash shell. The Tmux window function is useful because you can create multiple panes in a single window so that you can view all of the panes at the same time in one window, making it possible to use limited screen space efficiently.

In a Tmux session, the existing window will be listed below the screen. Shown is how the existing Windows are listed by default Tmux. There are a total of three windows, namely "Server", "editor" and "Shell".

To create a window, just press it Ctrl-b c , or to switch the window, just press Ctrl-b and then press the number that corresponds to the window you want to switch, which is displayed next to the window's first name.

Session

A Tmux session can contain multiple windows. Session functionality is very simple and easy to use, for example, you can create a dedicated TMUX session for a specific project. To create a new session, simply run the following command at the terminal:

Tmux new-s <name-of-my-session>

Suppose I still need to develop another project, so I'll create a new session for this. Although a new session was entered, the original session did not disappear. So I can go back to the previous session and work on it later. To create a new session, simply press Ctrl-b : and enter the following command:

New-s <name-of-my-new-session>
unless you explicitly close the session, the TMUX session will not disappear until you restart the computer. As long as you have not restarted your computer, you are free to jump from one project session to another. Switching between sessions in Tmux

To get a list of existing sessions, you can press Ctrl-b s . The list of sessions is shown:

Each session in the list has an ID that starts at 0. Press the corresponding ID to enter the session. If you have already created one or more sessions, but have not yet run Tmux, you can enter the following command to access an open session.

Tmux Attach
Quickly move the cursor between text, copy text

In ITerm2, I always dislike the fact that you have to use a keyboard and mouse to copy content quickly. I think there must be a faster way to copy without using a mouse. Fortunately, Tmux provides the ability to replicate only with the keyboard, since Tmux is started from the command line and cannot be used on the command line interface.

Move the cursor between text

You can move the cursor between text in a way that is very similar to Vim in Tmux. As you know, with the k key you can move the cursor to the previous line, use the w key to move backwards one word, and so on. The Tmux can also be set to VI mode to make it closer to Vim. To do this, you need to add the following configuration to the file ~/.tmux.conf .

# use Vim keybindings in copy modesetw-g Mode-keys VI
Send copied text to the system's clipboard

By default, when you copy text from Tmux, the copied text can only be pasted into the same Tmux session. To make the copied text paste anywhere, you need to have Tmux copy the text to the system's clipboard. To do this, we need to do this:

Install Retach-to-user-namespace. It will be very simple to install with brew, just run the following command:

$ Brew Install Reattach-to-user-namespace

in the configuration file ~/.tmux.conf Add the following:

# Invoke Reattach-to-user-namespace every time a new Window/pane opensset-option-g Default-command "Reattach-to-user-nam Espace-l Bash "
Select and copy text

Now that you have set up the VI mode and installed the Rettach-to-user-namespace, let's take a look at how to copy the text from the Tmux session. Suppose you want to copy an IP address, so we run the ifconfig command first. Next, follow these steps:

First press Ctrl-b [ enter copy mode, and then you can see a small highlighting of text appear in the upper right corner of the screen ("[0/0]") (as shown).

Next, you can j k move the l h cursor between the text in the same way as in Vim.

Move the cursor over the text you want to copy and then press the SPACEBAR to start selecting the text (which is exactly the same as the steps for copying text in Vim).

Select the text you want to copy and then press ENTER.

This allows the IP address to be copied and pasted anywhere.

Make copying text work more like Vim

You can also set Tmux to use v keys to select text and y copy text with keys. To do this, simply add the following configuration entry to the configuration file ~/.tmux.conf .

# Start selecting text typing ' V ' key (once you is in copy mode) bind-key-t vi-copy v begin-selection# Copy selected text To the system ' s clipboardbind-key-t vi-copy y copy-pipe "Reattach-to-user-namespace pbcopy"
Efficient pair-pair programming

You can share the address of the Tmux session to others so that they can access the session via SSH. Because the session is built on top of SSH, there is no additional overhead . By using a high-speed internet, for those who connect to a remote session, they feel that the session is running locally.

Using Tmate in Tmux

Tmate is a TMUX management tool that makes it easy to create Tmux sessions and share them with others over the Internet. To share a Tmux session by using Tmate, follow these steps:

    • Installing Homebrew
      $ RUBY-E "$ (curl-fssl https://raw.githubusercontent.com/Homebrew/install/master/install)"

    • Installing Tmate
      $ brew Update             &&brew tap nviennot/tmate &&brew Install mate
    • Use Tmate to open a new session
      $ tmate

    • Copies the SSH URL generated by tmate from the Tmux session. As shown, please note the information below the screen "[Tmate] Remote session:ssh ...":

    • With the URL you just copied, you can invite someone to access your session via SSH.

Once you understand how to leverage Tmux's pair programming capabilities, you can further enhance session interactivity with voice services from your favorite carrier.

Adjust the Tmux to enhance its integration with Vim to adjust the background of the color scheme

When I first opened vim through Tmux, I found that the color of vim was not displayed correctly. As shown, the background color is only where there are characters.

This problem is due to the need to configure a special terminal parameter (term parameter) to run Vim via Tmux. Please add this line of configuration to your ~/.vim file.

if exists (' $TMUX ')  set Term=screen-256colorendif

After updating the profile ~/.VIMRC, the color should be displayed correctly.

Adjust the shape of the cursor

By default, when Vim is run through TMUX, the cursor shape is the same regardless of whether the current vim is in insert mode, visual mode, or other mode. This makes it difficult to determine what the current Vim pattern is. To avoid this problem, you need to let Tmux notify iTerm to update the shape of the cursor. To do this, you need to add the following configuration to the file ~/.VIMRC.

if exists (' $ITERM _profile ')  if exists (' $TMUX ') let    &t_si = "<esc>[3 q" let    &t_ei = "<esc >[0 Q "  else let    &t_si =" <Esc>]50; cursorshape=1x7 "let    &t_ei =" <Esc>]50; cursorshape=0x7 "  endifend

Here I would like to thank Andy Fowler, who was the first to share the technique of adjusting the shape of the cursor.

Adjust text indent when pasting

You may encounter this problem when pasting text in Vim, and sometimes the indentation of the text changes, especially if you paste a large amount of text. Although you can solve this problem by executing before pasting :set nopaste , there is a better workaround here. is to add the following configuration to the configuration file ~/.vimrc , so that Vim automatically prevents the automatic indentation when pasting text.

"For Tmux-automatically set paste and Nopaste mode at the time pasting (as" happens in VIM UI) function! Wrapfortmux (s)  if!exists (' $TMUX ')    return a:s  endif let   tmux_start = "<Esc>Ptmux;"  Let Tmux_end = "<Esc>"   return Tmux_start. Substitute (a:s, "<Esc>", "<Esc><Esc>", ' G '). Tmux _endendfunction let &t_si. = Wrapfortmux ("<esc>[?2004h") let &t_ei. = Wrapfortmux ("<esc>[?2004l") function! Xtermpastebegin ()  set pastetoggle=<esc>[201~  set paste  return "" Endfunction Inoremap <special > <expr> <esc>[200~ xtermpastebegin ()

Here I would like to thank Marcin Kulik, who was the first to share this skill.

Other tools or tricks to enhance the Tmux experience Tmuxinator (automatically create a session for a project)

Suppose you are developing application A. During the development process, TMUX sessions are often created, with 3 windows of "Server", "editor" (for writing code) and "Shell" (for running system commands). Not only that, you also need to be temporarily in the development of application B at a certain time of the day. Then you have to create another session, albeit slightly different (such as a directory and some commands), but the session still contains the 3 windows in application A. But with Tmuxinator, you can declare a configuration for each Tmux session, and then you can create the session with 1 commands. That's a great feature, isn't it.

Tmuxinator is a Ruby gem package that you can use to create Tmux sessions. It works by defining the details in the session first in the configuration file, and then creating the sessions with 1 commands. Let's look at how to install Tmuxinator and how to add a configuration to open a session for a given project. You can install the Tmuxinator gem package by running the following command.

$ gem Install Tmuxinator

Once the Tmuxinator is installed, it can be run or ordered in the system Shell tmuxinator mux . Let's create a configuration file for the above application (with 3 windows, "Servers", "editor" and "Shell"). The purpose of the following command is to create and open a configuration file for this project.

$ tmuxinator New Project_a

When you press ENTER, the file is automatically opened ~/.tmuxinator/project_a.yml . To implement the configuration required for project A, you need to project_a.yml update the content to:

Name:project_aroot: <the-folder-of-project-A> windows:  -server: <command-to-start-application-server >   -Editor:    layout:even-horizontal    panes:      -Vim      -<command-to-launch-tests-guard>   -Shell: "

Once the above configuration has been added to the Yaml file of Project A, only the following command can be run to start the Tmux session.

$ tmuxinator Start project_a

Of course, if you want, you can also use the alias of the Tmuxinator command:

$ MUX Start project_a

It's done. Now, whenever you want to get into Project A's coding, you just need to run the Tmuxinator command.

You can check the official documentation for Tmuxinator here.

Beautify the Tmux status bar

By default, the Tmux status bar looks like this (the green bottom part of the figure):

We can change the appearance of the status bar as needed. For me, I like this refreshing look.

In order to achieve the effect, I added the following configuration to the configuration file ~/.tmux.conf.

# status Bar  # color  set-g status-bg black  set-g STATUS-FG white   # alignment  set-option-g status-justify centre
   # lower left corner  set-option-g status-left ' #[bg=black,fg=green][#[fg=cyan] #S #[fg=green]] '  set-option-g Status-left-length   # Window list  setw-g automatic-rename on  set-window-option-g Window-status-format ' #[dim ] #I: #[default] #W #[fg=grey,dim] '  set-window-option-g window-status-current-format ' #[fg=cyan,bold] #I #[fg= Blue]:#[fg=cyan] #W #[fg=dim] '   # lower right corner  set-g status-right ' #[fg=green][#[fg=cyan]%y-%m-%d#[fg=green] '

  

Summarize

In this article we first introduced the basic functions of Tmux, and then introduced the best features in Tmux. This later describes some of the configuration and several tools that can improve the TMUX experience. So what's your impression of Tmux? Have you found any other useful features or configurations? Please let us know if you have a message.

Thank you for reading this article!

Tmux Crash Tutorials: Tips and tweaks

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.