VI has three working modes: Command mode, insert mode, and edit mode.
Enter the command: vi + file name to start VI. The default mode is command mode.
Enter I to enter the insert mode, and Press ESC to return to the command mode.
Command mode
INSERT command
A insert text behind the cursor
A. Insert text at the end of a row
I insert text before the cursor
I insert text at the beginning of a row
O insert text under the cursor
O insert text on the cursor
Move cursor
H move to left
J move down
K move up
L move to the right
$ Move to the beginning of a row
0 to the end of the row
H jump to top
M jump to the middle end
L jump to the lower end
Jump to specific line
: Set nu
: Set Nonu cancel row number
Gg to the first line
G to the last line
Ng to n
: N to row n
DELETE command
X Delete cursor characters
NX Delete n characters after the cursor starts
Dd deletes the row where the cursor is located.
NDD deletes n rows after the cursor starts.
D. Delete the content starting from the cursor to the end of the line.
DG deletes the content from the beginning to the end of the cursor.
: N1, n2d Delete content from line N1 to line N2
Copy, cut, and paste
YY copy the current row
Nyy copies the cursor and N rows below
DD: Cut the current row
NDD cut the cursor and N rows below
P paste the cursor under the row
P paste to the row where the cursor is located
Replacement and Revocation
R replaces the character of the cursor
R starts from the character where the cursor is located and ends with pressing ESC.
U undo the previous operation
Search and replace
/Search for rows related to the keyword by keyword. Press n to view the next row and press n to view the previous row.
: Case Insensitive for set IC search
: Set noic disable ignore case sensitivity
: % S/old/new/g full-text replacement
: N1, N2s/old/new/g only replace N1 to N2
Note: The last g can be changed to C. C indicates whether to replace the G.
Save and exit
: WQ exit save type (shortcut: zz)
: Q exit without saving the input
: W save and do not exit
: W: Save the file name
Note: If you do not have the required permissions, add them later !, But only valid for root and owner