Before using vim, let's take an appetizer and learn some common commands in Vim. With these basic commands, we can use Vim more conveniently and speed up our work ~
Note: The following commands are mainly used in the normal mode of VIM.
1. Create/modify a file
When you need to edit an object, the object must first exist. You can use Vim to create or modify a file (assuming the file name is hahaya. CC), run Vim hahahaya. CC, this command will open hahaya. CC file, when hahaya. if the CC file does not exist, VIM will create the file and open it.
2. Open/save/exit/modify the file in VIM
(1): E filename <-> open the file filename. Note that filename contains the file path.
: E ~ /Hahaya. CC open the hahaya. CC file in the main directory
(2) Save the saveas filename <-> file as. Note that filename contains the file path.
(3): W <-> file storage
(4): q <-> do not save and exit. Return to the terminal.
(5): Q! <-> Force exit without saving
(6): WQ <-> Save and exit
(7): WQ! <-> Force save and exit
(8): BN <-> when opening multiple files, use this command to switch to the next file
(9): BP <-> when opening multiple files, use this command to switch to the previous file.
Three insertion Modes
As mentioned above, there are many methods to enter the insert mode from the normal mode. The following describes several common methods.
(1) I <-> Insert before the cursor to enter the insert mode
(2) A <-> Insert after the cursor to enter the insert mode
(3) O <-> insert a row after the current row to enter the insert mode
(4) o <-> insert a row before the current row to enter the insert mode
(5) CW <-> change word: Replace the character from the current position of the cursor to the end of the word. The actual effect is to delete it and enter the insert mode.
Four simple cursor Movement
(1) h <-> move the cursor one character forward
(2) j <-> move a row down
(3) k <-> move up a row
(4) L <-> move the cursor one character backward
(5) 0 <-> number 0, move to the line header of the row where the cursor is located
(6) ^ <-> move to the first position of the line that is not a blank character (the so-called blank character is space, tab, line feed, carriage return, etc)
(7) $ <-> move to the end of the row
(8) g _ <-> move to the last position of the line not the blank character
(9) G <-> move to the last line of this file
(10) ng <-> move to the nth line of this file
(11) Gg <-> move to the first line of this file
5. Copy/paste
(1) YY <-> copy the current row
(2) nyy <-> copy a total of N rows starting from the current row
(3) P <-> lowercase P: paste the copied content
Delete
(1) DD <-> Delete the current row and store the deleted row in the clipboard. All the rows can be pasted using P.
(2) NDD <-> delete a total of N rows starting from the current row, and store the deleted rows in the clipboard. All rows can be pasted using P.
7. Undo
(1) U <-> undo the last operation
(2) Ctrl-r <-> press Ctrl and R to undo the U.
There are indeed many commands in Vim, so it is very difficult to remember. Even if you remember it and don't need it, you will still forget it after a while. So the best way to learn Vim commands is: practice more, in this way, we will remember the vim commands ~