In the last section, we have basically understood the text editing Vim in Linux system and also know how to use Vim. In fact, at this time, we can already use Vim, but if we want to improve the efficiency of using Vim, we still need to master several groups of common commands of Vim.

Insert command

The insert command was briefly mentioned in the previous section, and there are three groups of six commands in total. Let’s review:

The command role
a Appends text after the cursor
A Append text at the end of the line
i Inserts text before the cursor
I Insert text at the beginning of the line
o Inserts a new line on the line below the cursor
O Insert a new line on top of the AD

For demonstration purposes, I’ll copy the /etc/services file to my test directory. Here we’ll use the cp command we learned earlier:

$ cp /etc/services test.services

After copying it, we use vi command to edit the file:

$ vi test.services

After entering the command enter, we can see a blinking cursor, we can use the arrow keys to move our cursor.

  • For example, let’s move the cursor to the image beloweThe position of the letter, and then insert some text after the letter, you can press lowercasea, then enter the character you want to insert, and pressEscExit editing mode:

  • We want to be innumberthenInsert the character before the letter and move the cursor tonAnd then press lowercasei, enter the character you want to insert:

  • If we want to insert a character at the end of the line where the cursor is, press uppercaseA, you can see the cursor automatically jumps to the end of the line:

  • If we want to insert a character on the line above the cursor, press uppercaseO, the button will automatically create a blank line above:

The same applies to the remaining commands o and I.

Ii. Location Command (1)
The command role
H or direction left key Move left one character
J or direction down key Down the line
K or up Move up a line
L or direction right click Move right one character
$ To the end of each line
0 Moved to the beginning of a line
H Move to the top of the screen
M Move to the center of the screen
L Move to the bottom of the screen

The above several positioning commands, you can try one by one in the learning environment.

3. Location Command (2)
The command role
:set nu Set the line Numbers
:set nonu Cancel the line Numbers
gg To the first line
G Go to the last line
nG To the first n rows
:n To the first n rows
  • The first command, mentioned in the previous section, sets the display line number

$ :set nu

After entering the command enter, the number of lines is displayed at the beginning of each line. To cancel the display, type set nonu.

  • Press capital on the keyboardG, quickly jump to the last line of the file:

As you can see, the cursor is now at the beginning of the last line, for a total of 11,474 lines in the file.

  • For example, now I want to quickly reach line 597. If I use the arrow to skip the line one by one, I don’t know when TO reach this line. At this time, WE can execute:

$ :597

When I hit enter, I’m immediately at line 597.

Delete command
The command role
x Deletes the character at the cursor position
nx Deletes n characters following the cursor position
dd Delete the cursor line, NDD delete n lines
dG Deletes the content at the end of the cursor line
D Delete from the cursor to the end of the line
:n1,,n2d Deletes rows in the specified range
  • For example, line 666 I need to delete characters9, just move the cursor to the location you want to delete and press lower casexYou can:

It was 99, but now it’s just a 9.

  • For example, to delete lines 685 to 686 now:

$ :685,686d

Copy and cut commands
The command role
Yy, Y Copy the current row
Nyy, nY Copy n lines below the current line
dd Cut current row
ndd Cut n lines below the current line
P, p Paste under or above the current cursor line
  • For example, if I want to copy line 4 and paste it under line 5 and above line 13, let’s look at what happened before:

First we move the cursor to line 4 and press uppercase Y or lowercase Y twice. Then we move the cursor to line 5 and press lowercase P to paste on the line below the original line 5. Then move the cursor to line 13 and press capital P to paste the line above line 13:

Replace and cancel commands
The command role
r Replaces the character at the cursor position
R Replace characters starting at the cursor, press Esc to finish
u Cancel the previous step
  • Let’s say we find row 4NetworkLetter of a wordNIf it’s wrong and needs to be corrected, we can pressiGo into insert mode, delete this character and then enter a new one, but there’s absolutely no need to bother. We can simply move the cursor to the character we want to modify and clickr, and then enter the character we want to modify.
Search and replace commands
The command role
/string Searches forward for the specified string; Ignore case when searching :set IC
n Searches for the next occurrence of the specified string
:%s/old/new/g The full text replaces the specified string
:n1,n2s/old/new/g Replaces the specified string in the specified line range
  • Let’s say we want to search for andtcpWhere relevant, type the following command:

$ /tcp

After you press Enter, the cursor will automatically locate to the position where TCP was first found:

If you type the letter N again, the cursor will move to the next position containing TCP.

  • Let’s say we want to put all the filestcpThe character is replaced byftpEnter the following command:

$ :%s/tcp/ftp/g

Press enter execution is completed, we can notice the original TCP string all became a FTP, we perform/TCP search, found that already can’t find any TCP relevant results:

Of course, the Vim command is not only these, we first master the above 7 kinds of Vim common basic commands, in our daily work process can normally use the Vim editor.