The original

As the saying goes, if you want to do a good job, you must first sharpen its tools. For programmers, if you want to code efficiently, it is very necessary to master the shortcut keys of a certain type of text editor (of course, the hand speed is also 😅). In the field of text editors, Vim and Emacs are two mountains that cannot be circumvented. There is a saying in the industry that Vim is the god of editors and Emacs is the editor of gods. This saying basically means that Vim is invincible in the control of editors, while those who can control Emacs are gods. There are a lot of stories about Emacs on the web, such as using Emacs to make coffee, but emacs can actually be developed in Lisp, so it can do more non-text editor things than Vim.

Psychological preparation

There is a learning curve in learning any skill, and it varies from skill to skill, but for those of you who are learning Vim, looking at the 90° learning curve below, you may need a cup of 82 coffee to calm down 😂.

Any enemy is a paper tiger
No other, but the hand is familiar

A profound

gird

To play Vim, you first need to enter Vim normal mode to get the sharp item. There are two ways to enter Normal mode, one is to hold down esc key, the other is to hold down CTRL +[key.

Handle mobile

If we use VIM to read the source code, this scenario will generally use normal mode. In normal mode, the corresponding shortcut keys up, down, left and right are KJHL, respectively. This is the most basic shortcut for using Vim, isn’t it particularly easy? In addition, when we browse the code, the need to move down the line is the most frequent, so we guess that VIM uses the J key as the move down. In addition, there is a small bump on the J key, which is especially suitable for a quick start. From this, we can see that viM’s key Settings are actually very humanized.

Cut the melon chopping vegetables

Many times when we write a first version of the application and do a self-code review, we often need to delete one or more lines. If we use a text editor like Notepad or Sublime, we need to select multiple lines and press the backspace key to delete them. With Vim, D2d can delete the current line and the next line. DND can delete the n-1 line from the current line.

The ground for

After reading the code, deleting and editing it, the most important thing to remember is to save it in real time, otherwise it will be useless in case of power failure or system crash. Unlike other editors that use meta+ S key to save, Vim needs to enter the command line mode, and the condition for entering the command mode is normal mode. Press: in Normal mode, and a small terminal window appears at the bottom of the editor. Type W to save the document, or x or wq to close the document immediately after saving it.

So we have completed vim three steps, browse > edit > save, how about, is not like playing a small game of general, looks not 90 degrees so difficult.

Advanced features

In the previous section, we looked at the three basic steps of using a text editor. Now let’s take a look at why Vim is called the god of editors, and what weird tricks it has and how powerful it is.

mobile

Moving around in a normal text editor is a pain. For basic movements like up, down, left, and right, we need to leave our typing area and run our fingers to the bottom right corner of the keyboard and press up, right, and left to move, which definitely takes us out of our comfort zone. However, vim’s Normal mode is a fun way to move around the comfort zone quickly and easily, with preset shortcuts provided by Vim. Before listing vim’s mobile keyboard shortcuts, let’s cover a few basic concepts:

  • level: Letters –> Words –> Sentences –> Paragraphs –> functions –> documents;
  • Fore and aft: words first — > tail lines — > first — > tail – > paragraph first — — > paragraph tail first — — > > document document tail;

The key shortcuts that move through VIm correspond to these concepts:

  • Letter level movement: see the vim gamepad shortcuts in the demoUp (K) down (J) Left (H) right (L);
  • Word level move: next word head (w), the end of the next word (e), the head of the previous word (b), the end of the last word (ge);
  • Row level movement: line head (0or^), end of line ($);
  • Paragraph level move: paragraph head ({), the end of the paragraph (});
  • Document level movement: Document header (gg), document end (G);

The editor

delete

Delete words and levels above are usually a combination key: d+ level key;

  • Delete character: Delete character before cursor (X), delete the character after the cursor (x);
  • Delete word: Delete the next word (dw), delete the previous word (db);
  • Delete row: Deletes the current rowddDelete the current line and the next line (dj), delete the current line and the previous line (dk);
  • Delete paragraph: Delete paragraph after cursor (d}), delete the pre-cursor paragraph (d{);
  • Delete document: Delete document before cursor (dgg), delete the document after the cursor (dG);

other

  • Cancellation:u(undo);
  • Repeat:ctrl+r(redo);
  • Copy: Similar to delete, copy is a combination key in the format of:Y + level(yank);
  • Paste:pandP(paste);

Rapid repetition

Movement + number + direction

Once you’ve familiarized yourself with most of the shortcuts for browsing and editing, use Vim’s quick repeat mode to speed things up. For example, if you want to scroll down four lines quickly and delete three words quickly to the right, if you only use the basic shortcut, you still have to repeat it many times, which is unacceptable for you to code efficiently. Vim provides a set of modes: action + times + direction, in which action is optional, for the requirements just need to use 4J and D3W to complete the requirements, is not very fast, come to try it!

Search/replace

Typically we use search for problem location and replace for refactoring. Vim also provides powerful search/replace functionality, and supports regular search and regular replace. To use vim’s search and replace, we need to enter the command mode from ViM’s Normal mode. Press: in normal mode to enter the command line mode.

  • Search:/key(Search down key) or? key(Search up key);
  • Replacement:%s/source/dest/gor1, 10 s/source/dest, including%sRepresents full text replacement,1, 10 sMeans that only rows 1-10 are replaced,sourceThe word being replaced,destThe word to be replaced,gIf the line matches multiple keywords, they will be replaced.
  • Regular replacement: this is the vim replacement inside the more advanced function, let’s demonstrate an example, the articlemorningking helloAre replaced withhello morningking, we need to type:%s/\(hello\) \(morningking\)/\2 \1/gThe capture group in the re is used here, isn’t it powerful?

File operations

Vim also provides file operations, such as save, exit, and discard, with the prerequisite of entering command line mode.

  • Save:w;
  • Exit:q;
  • Give up:q!

Save and exit can be used together, such as wq. Of course, you can also use the shortcut key X to save and exit

other

Limited space, the above listed the vim commonly used advanced features, of course, vim far more advanced features, such as can also use the macro, folding, select mode, split screen, bookmarks, buffers, etc., but because of the less commonly used, here a tentative differ a list, is interested in you can browse the refer to contains links to continue to navigate;

Live In Vim

I believe that if you are familiar with and efficient use of Vim, you will like to look for viM plug-ins in your accustomed software, after all, VIM plug-ins can make your finger quickly switch in the comfort zone, forced to sparkler. Here is to share with you the viM plug-in in the software I usually use:

  • Chrome: Vimium
  • Vscode: Plug-in -vim
  • Intellij Idea: IdeaVim
  • Mac: MacVim
  • Windows: GVim

Refer to the

  1. Vim mapping
  2. Oh my vim
  3. Vim Tutorial
  4. Vim manual