Hello, everyone, I’m Zheng Ge, improving the highest frequency elements can fundamentally improve the quality of life of the living body, for programmers, editing code is the high frequency elements, to improve this point, we need to use the most good at editing tools, which is not Vim. Linux and Mac come with Vim. Many ides, such as VSCode, PyCharm, Idea and Eclipse, also have Vim plug-ins. Vim is arguably the best editor. It’s incredibly hard to learn, but it’s incredibly easy to use, and even if it’s hard, it can be mastered quickly with two weeks of deliberate practice.

If you want to learn Vim, the best text editor ever created, as fast as you can, look no further.

This video won’t list all of them, just the ones that are most useful. Perfect for beginners to practice beginner Vim.

To be clear, don’t expect to be able to train Vim more efficiently than other editors in three days.

So let’s do this step by step.

First, learn to open, save and exit.

Understand Vim’s Normal mode and Insert mode.

The system enters the Normal mode. You can use h, L, J, k to move the cursor

     j

 h       l

     k
Copy the code

Dd: Deletes the current line and saves the deleted line to the clipboard.

I: Insert mode, press ESC to return to Normal mode.

X: Deletes a character where the cursor is currently located.

P: paste (actually paste from anonymous register, equivalent to “”p “)

:wq: save file + exit (:w save file, :q exit)

Second, it’s getting better

Various insertion modes

I: Insert before the cursor

A: Insert after the cursor

O: Inserts a new line after the current line

O: Inserts a new row before the current one

Cw: Replaces characters from the cursor position to the end of a word

Simply move the cursor

0: the number zero, to the wardrobe

$: to the end of this line

^ : To the first non-blank character on the line (blank characters are Spaces, tabs, newlines, carriage returns, etc.)

G_ : to the last non-blank character on the line

/pattern: Search pattern string (note: if more than one match is found, press n to next, n to previous)

Copy/Paste

Yw: Copy a word word word word word word

Y2w: Copy two words heaven on earth heaven on earth

P/p paste, p is after the current position, p is before the current position

Yy /Y: copies the current line

“+yy copies the current line to the system stickboard (“+ is the system stickboard register)

“+p Pastes the contents of the system paste board to the current position.

Undo/redo

u : undo

Ctrl+r: redo

Open/Save/Exit/Change the file (Buffer)

: w: inventory

:e : Opens a file

Saveas :saveas

: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 and :bp: You can have many files open at the same time, and use these two commands to switch to the next or the previous file.

Third, it can be better and faster

Repeat the previous command

: (decimal point) You can repeat the previous command

N: Repeat a command N times

Here is an example. To open a file you can try the following command:

2dd: Delete 2 lines

3p: Paste text three times

100iHello ESC: Will write 100 Hellos

hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello

: (decimal point) You can repeat the previous command. 3. : Repeat hello hello hello hello three times

Fast moving

To make your cursor movement more efficient, make sure you understand these commands and don’t skip them.

Ctrl F: Scroll down a page

Ctrl B: Scroll up a page

NG: to line N (note: G is capitalized in the command, and I usually use lines N to N, such as 137 to 137)

Gg: Go to the first line. (Note: equivalent to 1G, or :1)

G: To the last line.

Move by word:

W: To the beginning of the next word.

E: Go to the end of the next word.

If you think words are by default, use lowercase e and w. By default, a word consists of letters, numbers, and underscores

If you think words are delimited by blank characters, then you need to use uppercase E and W.

Now, let me talk about the strongest cursor movement:

% : matching bracket movement, including (.()()…) , {… }, […]. . (Note: You need to move the cursor over the brackets 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)

hello world hello world hello world hello world

These three commands are quite powerful for programmers.

Faster operation

You must remember the cursor movement, because many commands can be linked to the cursor movement command. Many commands can be written as follows:

For example, the 0y$command means:

0: First comes the wardrobe

Y: Copy it from here

$: copies to the last character on the line

You can copy the last character of the word from the current position by typing ye.

You can also type y2/foo to copy the string between the current position and the second “foo”.

The y command can also be replaced with:

D (delete)

V (Visual selection)

GU (change capitals)

Gu (Small write)

Visual selection is an interesting command, you can press V, then move the cursor, and you’ll see the text selected, and then, you can do D, you can do Y, you can do uppercase, etc

Fourth, use Vim advanced features.

Quick jump

Fa: To the next character of A, you can also fs to the next character of S.

T, : the first character before the comma. Commas can change into other characters.

3FA: Looks for the third occurrence of a on the current line. a,a,a,a

F and T: Same as F and T, but in opposite directions.

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 is in double quotes.

Vi “: will select foo.

Va “: selects “foo”.

Vi) : will select “foo”.

Va) : will select (“foo”).

V2i) : Map (+) (“foo”) will be selected

V2a) : will select (map (+) (“foo”))

Piece of operation:<C-v>(Ctrl + V)

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

# print("hello world")
# print("hello world")
# print("hello world")
print("hello world")
print("hello world")
Copy the code

With Vim on Windows, you need to use < c-q > instead of

, which is the copy clipboard.

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

Windows

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.

Example: Ask Vim to enter 1 to 100

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.Copy the code

@a: Write a 2 under a 1

@@ : Write 3 on the front of 2

Now on the command line typing 100@@ creates a new 100 lines.

If you find it helpful, please like, retweet, follow, thank you for your support.