More recent use in Linux environments, plus. NET Core learning, need to edit files under Linux, so you want to learn the legendary land of the most powerful editor? (I just heard about Vim and Escam fighting, I’m not responsible for this comment, because I only admit that the best IDE on land is VS). As we all know, Vim is a hard editor to get started and master, but once you master it, it’s hard to use another editor, HMM… In line with the mentality of learning and learning, I collected and sorted out the materials of Vim on the Internet and wrote an article about Mark based on my own learning experience.

PS: If you want to learn Vim, and do not have a Linux environment, you can use the Ubuntu subsystem of Win10.

Vim taps on the door

Vim as an editor, so the basic function of course is to edit, so first use its editing function.

The editing function needs the following common commands:

  • i→ Insert mode, press ESC to return to Normal mode.
  • x→ Delete a character where the cursor is.
  • :wq→ Save disk + exit (: W save disk, :q exit) :w can follow the file name
  • dd→ Delete the current line, and the deleted line to the clipboard
  • p→ Paste the clipboard

In addition, there are two useful features:

  • hjkl(Strongly recommended, but not necessary) → You can also use cursor keys (←↓↑→). Note: j is like the down arrow.
  • :help <command>→ Show the help of related commands. You can also type :help without the command. (To exit help, enter :q)

The two Vim modes mentioned above are Insert mode and Normal mode. How to distinguish the current mode can be found in the annotation in the lower left corner of the terminal.

Vim on zen house

After mastering the basics of editing, you can start to learn more about commands by following the following categories :(all the following are in Normal mode, just press ESC, mm ~ o( ̄▽ ̄)o

Various insertion modes

  • a→ Insert after the cursor
  • o→ Insert a new row after the current row
  • O→ Insert a new row before the current row
  • cw→ Replace the characters from the cursor position to the end of a word

Move the cursor

  • 0→ number zero, to the wardrobe
  • ^→ Go to the first blank character on the line that is not blank.
  • $→ To the end of the line
  • g_→ Go to the last position on the line that is not a blank character.
  • /pattern→ Search the string of pattern (if multiple matches are found, press n to go to the next one)

Copy/Paste

  • P→ Paste lowercase P after the current position, uppercase P before the current position
  • yy→ Copy the current line to ddP

Undo/Redo

  • u– > undo
  • <C-r>To redo

What? Don’t know what Undo and Redo are? Undo and restore! ╯▔ dish ▔ ╯ to learn English!

Open/save/Exit/change the file

  • :e <path/to/file>→ Open a file
  • :wTo save
  • :saveas <path/to/file>– > save as<path/to/file>
  • :x, ZZ or :wq → save and exit (:x means save only when needed, ZZ does not need to enter a colon and press enter)
  • :q!→ Exit without saving: QA! Forcibly exit all files being edited, even if other files have changed.
  • :bn:bp→ You can open many files at the same time, use these two commands to switch to the next or the previous file.

Mastering the above commands, you’ll basically be able to use Vim just like any other editor, doesn’t it feel like so much work that in the end it feels like notepad uses sigma (flying ° д °;) っ? It’s okay, take your time!

Vim’s winding path leads to a secluded place

Land is the strongest editor can stop here, see what advanced operation bar, this part of the command Vi also use ( ̄▽ ̄)”!

Repetitive operation

  • .→ (decimal point) You can repeat the previous command
  • N<command>→ Repeat a command N times

A few examples:

  • 2dd→ Delete 2 lines
  • 3p→ Paste text 3 times
  • 100idesu [ESC]→ “Desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu Desu desu desu desu desu
  • .→ Repeat the previous command — 100 “desu”.
  • 3.→ Repeat “desu” 3 times (note: not 300, see how smart VIM is).

Move the cursor efficiently

  • NG→ To the NTH line is also generally used:NTo the NTH row, so: 137The line 137
  • ggLambda goes to the first row1GOr,: 1.
  • G→ To the last line.
  • w→ Go to the beginning of the next word.
  • e→ Go to the end of the next word.

If the word is the default, use lowercase e and w. By default, a word consists of letters, numbers, and underscores (like program variables); If the word is delimited by a blank character, then you need to use uppercase E and W. (a program statement, for example)

  • %: matches bracket movement, including (, {, [. Requires the cursor to move over the bracket first

  • * and #: match the word where the cursor is currently located, move the cursor to the next (or previous) match word (* is next, # is previous)

More than cursor movement




For example, the 0y$command means:

  • 0→ First to the wardrobe
  • y→ Copy from here
  • $→ Copy to last character on line

Copy from the beginning of the line to the end of the line, and optionally combine other uses:

  • For example, you can enteryeFrom the current position to the last character of the word.
  • You can also typey2/fooTo copy the string between two “foo”.

You don’t have to press y to copy, the following command will also be copied:

  • d(delete)
  • v(Visual selection)
  • gU(uppercase)
  • gu(Write smaller)

These commands will store the content in the clipboard, where V visual selection means moving the cursor after pressing V, and then performing D/Y /gU operations after selecting the content.

Vim goes even further

Impressed by the ease of operation? (●’◡’●), sit down. Just a basic operation: one item which really makes Vim a plus-one is yet to come.

Moves the cursor over the current line

  • 0– to the wardrobe
  • ^→ First non-blank character on the line
  • $And to the end of each line
  • g_→ Go to the last position on the line that is not a blank character.
  • fa→ Go to the next character of A, you can also fs to the next character of S.
  • t,→ To the first character before the comma. Commas can change into other characters.
  • 3fa→ Look for the third occurrence of a in the current line.
  • FT– > andftSame thing, but in the opposite direction.

Another useful command is dt” → delete everything until you hit double quotes — “.

Area selection

In Visual mode, these commands are powerful and are formatted as

The < action > a < object > and < action > I < object >

Action can be any command, such as d (delete), y (copy), or v (mode optional). Object may be: w a word, w a space-separated word, S a sentence, and P a paragraph. It can also be a special character: “, ‘,),},].

Suppose you have a string (map (+) (“foo”)). The cursor key is at the first o position.

  • vi"→ will select foo.
  • va"→ will select “foo”.
  • vi)→ will select “foo”.
  • va)→ Will select (“foo”).
  • v2i)→ Map (+) (“foo”)
  • v2a)→ Will select (map (+) (“foo”))

Block operation

Block operation, typical operation: 0 < c-v > < c-d > I– [ESC] //

refers to holding down the Ctrl function key

  • ^– to the wardrobe
  • <C-v>→ Start block operation
  • <C-d>→ Move down (you can also use HJKL to move the cursor, or use %, or whatever)
  • I-- [ESC]→ I is insert, insert “–“, press ESC to effect each line.

Automatic prompt

< C – n > and < C – p >

In Insert mode, you can type the beginning of a word and then press < c-p > or < c-n > to complete the word automatically.

Macro recording

Qa operation sequence Q, @ A, @@

Qa records your operation in register A. So @ A will replay the recorded macro. @@ is a shortcut used to replay newly recorded macros.

International practice, for example:

In a single line of text with only “1”, type the following command:

  • qaYp<C-a>q

Qa starts recording Yp copy lines. < c-a > adds 1.q to stop recording.

  • @a→ Write a 2 under a 1
  • @ @→ Write 3 in front of 2. Now do it100 @ @A new 100 rows are created and the data is increased to 103.

Visual selection

v,V,<C-v>

Earlier, we saw an example of < c-v > (< c-q > under Windows), and we can use v and v. Once you’ve been selected, here’s what you can do:

  • J→ Join all the lines together (into a single line)
  • <>→ Left and right indent
  • =→ Auto indent

Add something after all the selected lines:

  • <C-v>Select the relevant row (you can use j or /pattern or %…)
  • $Line to the last
  • AInput string.Press ESC.

Split screen

The split and: vsplit

Here’s the main command, which you can use VIM’s help with :help split.

  • :split→ Create split screen (:vsplit creates vertical split screen)
  • <C-w><dir>: Dir is the direction. It can be either HJKL or ←↓↑→, which is used to switch the split screen.
  • < C - w > _ (or < C - w > |): the maximum size (vertical split screen |)
  • > < C - w + (or < > C - w -): Increase size

Master the above instructions and methods, even if the basic understanding of the use of Vim, but also to VScode and VisualStudio installed Vim plug-in to make Vim operation increasingly skilled, really master Vim, you can easily use the keyboard to complete the editing, without moving the mouse!