There are three modes of vim: general mode, editing mode, and command mode.
Normal mode : When vim filenameediting a file, it is normal mode as soon as you enter the file. In this mode, you can do the following operations: move the cursor up and down; delete a character; delete a line; copy, paste one or more lines.
Edit mode : In normal mode, it is not possible to modify a character. From normal mode to edit mode, you just need to press a key (i, I, a, A, o, O, r, R). When entering edit mode, the words "INSERT" or "REPLACE" will appear on the bottom line of the screen. From edit mode back to normal mode, just press the ESC key at the top left of the keyboard.
Command mode : In normal mode, enter ":" or "/" to enter command mode. In this mode, you can search for a character or string, save, replace, exit, display line numbers, etc.
Press the number and then press space/l, the cursor moves to the right by n characters; if the line of characters is less than n, the cursor continues to move to the right from the bottom line until n
Copy, cut and paste in normal mode
button
behavioral manipulation
dd
delete/cut the line where the cursor is
ndd (n is a number)
Delete/cut n lines starting from the line where the cursor is located
yy
Copy the line where the cursor is located
nyy
Start at the line where the cursor is and copy down n lines
p
From the line where the cursor is located, paste the copied content down
P
Paste the copied content upwards, starting from the line where the cursor is located
u
Redo the previous operation
Ctrl+r
Cancel the restore of the previous step
x
Delete/cut one character backward
X
Delete/cut one character forward
v
After pressing v, moving the cursor will select the specified character, and then you can copy, cut and other operations of the specified character
enter edit mode
button
behavioral manipulation
i
Insert character before current character
I
Insert character at the beginning of the current line
O
Insert a new line below the current line
O
Insert a new row on the current row
a
Insert character after current character
A
Insert character at the end of the current line
command mode
button
behavioral manipulation
/word
Look for a string named word behind the cursor, when the first word is found, press "n" to continue searching for the next one, and "Shift+n" to search for the previous one
?word
Look for a string named word before the cursor, when the first word is found, press "n" to continue searching for the previous one
:n1,n2s/word1/word2/g
Find the string word1 between lines n1 and n2 and replace it with word2
:1,$s/word1/word2/g
From the first line to the last line, find word1 and replace with word2
:1,$s/word1/word2/gc
The effect of adding c is to require user confirmation before replacing
If the string to be searched/replaced contains a "\" symbol, you can add a "/" symbol before the "\" symbol, for example, to find "/etc/hosts", you can do this with "/\/etc\/hosts". When searching and replacing a string, the string contains the "\" symbol. You can also replace "/" with "#" or "@", such as ":1,$s#word1#word2#g".
button
behavioral manipulation
: set no
Display line number at the beginning of each line
:set nonu
cancel line number
:nohl
unhighlight
:w
save edited text
:w!
If the text property is read-only, force saving
:q
quit
:q!
Exit without saving, whether edited or unedited
:wq
save and exit
:x
Execute ":x" after editing the file, the effect is the same as ":wq". If you open the file for viewing, and nothing has changed, ":wq" will change mtime, and ":x" will not change mtime.
Practice with Vim
[root@centos-01 ~]# cp /etc/dnsmasq.conf /tmp/1.txt
[root@centos-01 ~]# vim /tmp/1.txt
Move down, right, left, and up by 6 characters ( 6j, 6l, 6h, 6k)