You often need to modify some files in Ubuntu. Here we will summarize some common operations in VI.
1. Enter the VI command
VI filename: open or create a file and place the cursor at the beginning of the first line
After entering the file, it is in command mode)
2. Switch to the command line mode.
Press ESC
3. insert mode)
In command mode, press the letter
I insert before the cursor;
A is inserted after the cursor;
I insert at the beginning of the row where the cursor is located;
A is inserted at the end of the row where the cursor is located;
O insert a row above the row where the cursor is located;
O insert a row under the row where the cursor is located;
S. delete a character after the cursor and enter the insert mode;
S. Delete the row where the cursor is located and enter the insert mode;
4. Deletion Mode
In command mode, press the letter
X deletes a character from the cursor;
Dd deletes a row;
# X delete several characters, # indicates a number, for example, 3x;
DW deletes a word;
# DW deletes several words. # It is represented by numbers. For example, 3dw deletes three words;
# Dd deletes multiple rows and # represents numbers. For example, 3DD deletes the cursor row and the cursor's next two rows;
D $ Delete the content from the cursor to the end of the line;
J. Clear the space between the row where the cursor is located and the previous line, and link the cursor row with the previous line;
5. Replacement
In command mode, press the letter
R replaces the character at the cursor
R replaces the character wherever the cursor goes until you press the ESC key.
6. Exit
: WQ! Or: X Save the modification and exit.
: Q! Exit Without saving changes