Recently, I read two books about Vim and learned vim again. While feeling the strength of ViM, I also felt ashamed of my previous complacency. I picked up my notes about ViM and reorganized them and shared them on the Internet as a series. The main purpose is to enable more viM fans to master more operating skills.

The purpose of this series of ViM notes is not to teach you how to use Vim from scratch, but rather to serve as a manual or to expand your horizons so that you know that vim can be used in this way.

Hopefully, you found some of the things that interest you in this tutorial series!

This tutorial series is divided into four parts:

  1. Divine editor Vim uses – 1. Basics
  2. The divine editor Vim uses -2. Operation section
  3. The divine editor Vim uses – 3. Plugins
  4. The divine editor Vim uses -4. Regular operations
  5. The divine editor Vim uses -5. Finally

Whether VIm is worth learning

There are plenty of online descriptions of how steep vim’s learning curve has been. People are most afraid of unknown things. If someone told you that you could learn the basics by spending a day finding a how-to tutorial, would you learn Vim? The vast majority of people are able to, in fact, that is the case, if only the basic operation of 1 day time is absolutely enough. But most people don’t get that answer, so they close the window.

In my opinion, the cost performance of learning input and output of VIM is very high. After learning ViM, you will have a series of benefits as follows:

  • Vim is based on terminals. After mastering VIM, you will have a deeper understanding of terminals
  • Vim can combine other tools in the terminal to form your tool chain (e.ggit)
  • inAtom.Sublime.VSCode.NOTEPAD++Today, you can opt for a time-tested editor that’s nearly 40 years old
  • Operating a remote server often requires text editing in a terminal, and VIm allows you to tread on the ground
  • Vim supports all platforms, so you can use one profile for all platforms
  • With 13

Editor switching comes at a cost, and not a small one. The loss of doing something else after half done is definitely much greater than that of a person who has been doing it all the time. Therefore, it is prudent to choose an editor suitable for yourself as a partner in the future. However, people who have used Vim seldom switch to another editor, which should explain a lot.

Some people take the tool-dead view that writing good code is everything, and it is. But would anyone refuse to write elegant code faster and more efficiently? We don’t have to have our cake and eat it. We can have both.

This topic is easier to lead to war, point to the end, specific how to hand over to the reader to judge.

Let’s go!

The installation

The first step, of course, is installation, which is demonstrated here for MacOS and Linux

MacOS

Installation of different versions

  1. The built-in

    Vim is built into Mac OS by default. If you do not have any other versions of VIM installed, type vim –version in the terminal to see the following image:

    + indicates included functions, and – indicates not included functions. It can be seen that the built-in Vim clipboard option is -, which means that there is no clipboard function, that is, there is no interaction with the system clipboard, that is, the content in Vim cannot be copied to another program, and it cannot be changed no matter how Settings are set. That’s the big difference.

    And since it is built into the system, it is impossible to manually upgrade it until Apple updates the official built-in VIm when the system is updated.

  2. Official Vim, installed via Homebrew

    You can use the package management tool Homebrew to install the official version of vim. Install the vim in /usr/local/bin/vim, and install the vim in /usr/bin/vim. The sequence of PATH, the environment variable identified by the system, must be set to

    /usr/local/bin
    /usr/bin
    /bin
    /usr/sbin
    /sbin
    Copy the code

    To adjust the PATH sequence, open the /private/etc/paths PATH (this is a hidden PATH, you need to turn on the show hidden files switch to see), use any text editor to open and save the changes. The diagram below:

    When vim is entered on the terminal, the system searches for /usr/local/bin first, and /usr/bin if it does not exist, and so on. Verify which vim again. If /usr/local/bin is returned, the setting is successful. Use vim [filename] to install vim from Homebrew.

    At this point, you can see the following figure via vim –version

    The biggest change is that the Clipboard option has changed to +, and many functions have also changed to +.

    Everything is fine with Vim installed via Homebrew, and I’ve been using it for a long time, but two problems have been bothering me:

    1. There is a big delay between insertion mode and normal mode with the input plug-in, for example, there is a 2-3 second delay after EXITING input mode, during which if I operate any key on the keyboard, the result will be typed into the input method. The experience is terrible.
    2. Speed. Longer in editingMarkdownIf syntax rendering is enabled and the bottom status bar plugin is enabled, there will be a large delay in moving the cursor quickly. This feeling is most evident when the grammar paragraph is complex and the cursor moves quickly. Greatly affected the use of feeling.

    If vim continues to be such a problem, it’s not an editor god. I searched the Internet and realized that a lot of other people have noticed this as well. Vim on Mac terminals is slow, stuttering, and on Linux it runs smoothly without any latency. Through multiple checks, I tested MacVim’s fluency on the Mac. There was a surprise.

  3. MacVim, installed via Homebrew

    MacVim is a port of Vim for Mac systems, and its kernel is consistent with the official version. This means that if the official version of Vim can do something on the Mac, it can do the same thing on MacVim.

    To install via Homebrew, simply use brew install macvim. After the installation, use mvim on the terminal to open the MacVim desktop program.

    To open the command-line version of MacVim from a terminal, use mvim -v [filename].

    Note: Since the vast majority of packages installed through Homebrew do not have a GUI, Homebrew does not move packages into /Applications, which makes it difficult to double-click a file on the desktop to open it using MacVim. And you won’t find MacVim on the Mac LaunchPad (even though it’s GUI software).

    So, here’s the next solution.

  4. MacVim, installed via Homebrew Cask

    Cask is a software manager for Homebrew. Homebrew itself is already a package management tool, its Cask can manage procedures, a layer of a layer, really play. But the vast majority of Homebrew packages are environments, not interactive software for everyday users. Most Cask packages are GUI software. Through Cask, we can install most of the software we need daily, such as QQ, music, browser, various tools…

    Install the cask package through Brew Install Cask. Brew Install Macvim –cask to install macVim.

    MacVim automatically binds the mvim command to vim, gvim, view, and other commands in /usr/local/bin. This means that MacVim desktop will be opened whenever vim, gvim, or view is used when PATH is /usr/local/bin first.

    If you still want to use MacVim to open the file in command line mode, you can use mvim -v or vim -v or several other bound commands. Of course I set the alias directly in ~/.zshrc (I’m using ZSH) for convenience.

    alias vim='mvim -v'
    Copy the code

    Use source ~/.zshrc or restart the terminal to override ~/.zshrc

Comparison and summary

Through the comparison of MacVim and Terminal ViM installation experience, the following advantages and disadvantages of MacVim (also corresponding to viM’s advantages and disadvantages) are summarized:

  1. advantages

    • There’s a system level copy and paste undo shortcut,
    • Scrolling is smoother,
    • Input method switching speed is faster,
    • The navigation slide on the right also comes into play
  2. disadvantages

    • Not being integrated in terminals means the cost of switching to and from terminals is higher
  3. conclusion

    If you have to use Vim on a Mac and you need a lot of fluency, use MacVim.

    If most of the work is done on a terminal, use the terminal VIm.

    If both are required, just wait until the official Mac system resolves the latency issue

CentOS

In CentOS, you can install VIM using the following methods

  • The source code to compile
  • Yum install
  • GUI version of ViM installation

But after a lot of experimentation, the first two of these methods are viM with clipboard missing, meaning you can’t interact with the system clipboard, so it’s best to install the GUI version of ViM

sudo yum install vim-X11
Copy the code

Use vIMx instead of vim when calling vim

Debian && Ubuntu

Install vim-gTK or vim-gnome.

Arch Linux

Install the install gvim

Vim mode differentiation

Mode is one of the most important differences between Vim and other editors. Simply put, the same operation on the same interface can produce different effects in different modes. The most common modes are Normal Mode, Insert Mode, Visual Mode, and Command Line Mode

Normal mode (Normal mode)

Vim default mode, also known as command mode, can be used for movement and simple editing using HJKL

Insert Mode

In this mode, you can input as much as you like. In normal mode, you can enter the insert mode through I, A, O, cc and other commands.

Visual Mode

Similar to normal mode, except that the current selection is expanded when moving. Common mode Press V (to enter character selection mode) or V (to enter line selection mode) to enter this mode

Command Line Mode

In this mode, you can enter a command line at the bottom of the window and execute it. When a command is executed, the system automatically exits the command mode and enters the common mode.

Command mode You can enable the command by entering:

Replace Mode

The newly entered text replaces the text at which the cursor is located and moves the cursor backward in turn.

In common mode, press R to enter the mode

Ex Mode

The command mode is similar to the command mode, except that the command can be executed multiple times without exiting the command mode automatically. Exit the mode until :visual is typed

You can enter this mode by pressing Q in normal mode

Vim key mapping

When we write a script or don’t like the way certain buttons are triggered, we can of course change them to the one we think works best for us. Vim has made a very fine distinction between key mappings to give us more control over the situation.

Key table

  • <k0>-<k9>: Keypad numbers 0 to 9
  • <S-x>: capital S with x, meaning shift+ X key combination
  • <C-x>: Capital C with x means CTRL + X
  • <A-x>: capital A with x, meaning Alt + X key combination
  • <ESC>: ESC
  • <BS>: Backspace Backspace key
  • <CR>: ENTER ENTER
  • <Space>: the blank space key
  • <Shift>: the shift key
  • <Ctrl>: the CTRL key
  • <Alt>: the Alt key
  • <F1>-<F12>: function keys F1 to F12

The prefix

Because Vim has multiple modes, we can set up different shortcut mappings for different modes

  • Map: for normal mode, but mode, select mode, operate pending mode

  • Nmap: used in normal mode

  • Vmap: For visual mode and selection mode

  • Xmap: For visual mode

  • Smap: Used to select a mode

  • Omap: Used to operate pending mode

  • map! : Used to insert mode and command line mode

  • Imap: For insert mode

  • Cmap: used in command line mode

  • Nore: No recursion, otherwise pressing I under the key mapping below is equal to pressing K, which is equal to pressing J, resulting in a mess.

    nnoremap  i   k
    nnoremap  k  j
    nnoremap  j   h
    Copy the code

  • : this means that the various shortcut keys are preceded by

    to avoid ambiguity. As shown below, the file is saved when ##w is pressed in normal mode (the default leader key is backslash \ if no customization is done)

    let mapleader="# #"
    nmap `<leader>` w    :w<CR>
    Copy the code

Actual Modification Examples

inoremap ' ''
      
        I "single quote completion // In normal mode, press fw quickly to type: W 
       
        , fw can interpret as file\_write. "Save and exit file nmap fw :w '
        
         ' nmap fq :q '
         
          ' nmap FWQ :wq '
          
         
        
       
      Copy the code

The last

The author of this article is Hanley Lee, first published in Shining Journey. If you agree with this article, please Follow