Personal site: Taoyuan

The foreword 0.

0.1 introduction

Vim is known as the “editor god”, while emacs, his contemporary, is known as the “Editor of God”. It can be seen that Vim’s status in the editor is very high, thanks to Vim’s fingering, knocking code like flowing cloud. In particular, I admire the founders for creating such a convenient fingering method of code, and this article is to take you into the pit of vim fingering operation.

0.2 Why should I learn Vim

In this day and age of front-end development tools (VS Code, Sublime, Atom, IDE Webstorm), why am I talking about something that was developed over 20 years ago? What I want to say here is that there is no less value in being old, and once you get used to the vim fingering, it’s hard to write code without vim, and you don’t even know how to do it. While writing code on an editor that supports Vim fingering can feel like a breeze, a mechanical keyboard is a treat, particularly geeky.

Once you learn vim’s fingering, it will pay off for the rest of your life, at least while you’re typing code. Quite literally, it will get you out of the annoying, repetitive movement of the mouse while you’re typing code, which is one of vim’s design concepts — no mouse.

0.3 Article for readers

This article is suitable for front-end developers, because I’ve only done some front-end development with Vim, and I don’t have a say in other areas.

0.4 Q&A

Q: Are you recommending vim as a way for us to completely abandon our previous editors and embrace Vim?

A: Of course not. First of all, the learning cost of Vim is very high, because it is fundamentally different from the way we use the editor to type code, because it does not use the mouse, pure keyboard operation. Think about how slow it would be to type code if you didn’t use the mouse in your VS code, you used the arrow keys all the way up, down, left and right. And it’s hard for beginners to make vim their own IDE. So what I recommend here, whether it’s your VS Code, Sublime, etc., is to install a Vim plugin. I tried it on VS Code and it was great, but now I’m in the arms of Emacs.

Even though I’m using Emacs, I still install vim (Evil). If you’re interested, you can recommend Spacemacs next time.

The opening said so much, just want to attract you, think vim’s fingering is worth learning, then enter the main topic.

1 mnemonics

All of the shortcuts defined on Vim have some meaning, and I’ll list some of them here.

1.1 Regular expression correlation

  • $: Moves the cursor to the end of the line
  • ^ : Moves the cursor to the first letter of the current line.

1.2 Movement (Motion)

H (left), L (right), J (up), K (down), F (front), B (back), U (up)

1.3 Operator

D (delete), I (insert), A (append), C (change), y (copy), P (paste)

1.4 Common abbreviations

W (word), S (sentence)

Understand the three common patterns in VIM

There are several modes in Vim, but since I’m looking at plugins in the current editor, I won’t include commands for saving and exiting files.

  1. Nomal mode (default mode, because this mode is used in most scenarios, all will be the default mode).
  2. Insert mode – use it sparsparly and vim becomes “dumb”
  3. Visual Mode (Visual mode, it might make sense to call it cursor mode or select mode)

2.1 Conversion relationship between the three modes

Article 3 tutorial

Once we have memorized the mnemonic above, we can act as if we were writing English phrases.

Vim shortcut syntax: [operator][count][motion] (d2w)

3.1 Moving the Cursor

3.1.1 The most basic H, J, K, L

The basic upside-down left/right move (same as the upside-down left/right keyboard, except that the arrow keys are located in the lower right side of the keyboard, far away from the main keyboard area, which is also relatively advantageous) :

  • H: Move the cursor to the left
  • J: The cursor moves down
  • K: Move the cursor up
  • L: Move the cursor to the right

Move is part of motion, so you can move multiple rows by adding “count” in front of it, so if you move 10 rows up, you can move 10K. Often the editor will have a line number and you need to add or subtract to locate it. This is handy if you are using a relative line number. Those who do not understand the relative line numbers can see the picture below.

3.1.2 Fast movement

W: Move the cursor forward by one word B: Move the cursor back by one word 0: Move the cursor to the first letter of the current line ^ : Move the cursor to the first letter of the current line (note the difference between 0 and 0) $: Move the cursor to the end of the line FX: Move the cursor to the next x of the current line (x is any letter) tx: Similar to the above command, move to the left of x) : Move cursor to the next sentence (: move cursor to previous sentence {: Move cursor to previous paragraph} : Move cursor to next paragraph

3.1.3 Quick Location

I’ve just shown you how to move the cursor to a line by relative line number, and how to move the cursor by absolute position. That’s gg. Grammar: [num] gg

  • 2gg: Cursor moves to line 2
  • Gg: move the cursor to the beginning of the file
  • G: Move to file stomach
  • H: Move to the top of the screen
  • M: Move to the middle of the screen
  • L: Move to the bottom of the screen

Bookmarking: This feature is also very convenient. Few editor features are available. It is highly recommended to list it separately.

  • M: Bookmark the current line with x being any letter (m is mark)
  • ‘: The single quotation mark is adjusted to the line marked just now by adding the letter after m

3.1.3 Scrolling

  • Ctrl + B: Move one screen up (Foward lowercase)
  • Ctrl + F: Move one screen down (lower case with Backward)
  • Ctrl + D: Scroll Down half screen (lowercase letter Down)
  • Ctrl + U: Scroll Up half screen (Up starts with a lowercase letter)

3.2 Cut, Copy, and Paste

  • D [n] W: Cuts the next n words. Dw: cuts the current word
  • [n]dd: Cuts n lines. Dd indicates the current line
  • D: Cut the content after the cursor to the end of the line
  • Yy /Y: copies the contents of the current row
  • P: Paste it behind the cursor
  • P: Paste in front of the cursor

In normal mode, there is no deletion operation. D This deletion is also a cut.

All above are in normal mode

3.3 Editing Mode

As I said earlier, when you enter edit mode, you become a “dumb” vim, just like a normal editor. A lot of people know that you can get in with I, and there are a lot of commands that you can use to get in smart.

  • I: Insert at the cursor (insert lowercase)
  • I: Insert at the beginning of the line
  • A: Insert one character after the cursor (append starts with lowercase)
  • A: Insert at the end of the line (uppercase append)
  • O: Insert on the previous line
  • O: Insert on the next line
  • S: Clears the current character and enters insert mode
  • Cc /S: Clears the current row and enters insert mode
  • Cw: Clear the current word and enter insert mode

3.4 Visual Mode

Once you’ve learned everything, the visual mode is easy, and you’ll be using it a lot. I’ll call this the “choice mode,” and let’s explore how it works.

There are only two key points: v (character selection) and V (line selection)

3.4.1 V (Lowercase V)

In Normal mode, press V, then press L, press L repeatedly, you will see that the right side is always selected (highlighted). The same operation, press V, and then practice pressing H, J, k.

What is the purpose of the selection? Just to do some editing, such as delete D (essentially cut), copy y.

As a tip, it’s always a good idea to use C after you’ve selected it, so cut and go into insert mode.

3.4.2 V (Capital V)

V is just to select the row, which is also easy. After pressing V, and then j, K, you can select up and down the row, and then delete d, copy y, it is more convenient.

3.5 Combined Operations

This function is more powerful, very applicable, highly recommended. To remember first formula: operator + I | a + scope

Operator is the insert (c), cut (d), copy (y), and select (v) we mentioned earlier. I means within scope, and a means include the scope tag. Scope is the scope of the operation.

Example: if I is replaced by a, the symbol is included

  • Vib (check what’s in the brackets)
  • ViB (check the braces)
  • Vi “(select the content inside the double quotation marks)
  • Vi ‘(select the content inside the single quote)
  • Vi < (check the Angle brackets)

3.6 Recall, Search, and Replace

  • U: undo last action (same as command + z)
  • / |? XXX: search for a string matching XXX throughout the document, / means search down,? Represents upward lookup, where XXX can be a normal expression. When you find it, enter n to find the next match, enter n to find the opposite direction.
  • :%s/original/replacement: Retrieves the first “original” string and replaces it with “replacement”
  • :%s/original/replacement/g: Retrieve and replace all “originals” with “replacement”
  • : % s/what/replacement/gc: search out all the “what” string, but in front of the replaced with “replacement”, first ask whether replacement

These operations are generally included in the editor has a good shortcut keys, not to remember what.

4 conclusion

, I write the purpose of this article is not to summarize what knowledge (in fact, when you vim with ripe, these commands fully formed muscle memory, no down), or want to send the article, I just want to do a publicity for vim, let more people contact with vim, let more developers in the development of more efficient. If you find this article helpful, please forward it to more people and let them know about it.

Finally, I want to remind you that, for these shortcut keys, it is difficult to remember by rote, according to the way I summed up some semantic words to remember, there is a formula to remember the formula, may be a little faster, and then is more practice, knock more yourself to remember. When I first started learning, I didn’t know there were mnemonics, so it was basically rote memorization. I just used a piece of paper, and I copied the commands into a handwritten paper, and I put them next to my computer.

Good luck!

Hello everyone, I am Taoweng, I speak for myself!

Personal wechat public account, after trying to adhere to a weekly dry goods

How to use VIM to improve development efficiency