VI commandIt can be said that it is the most common command to edit files in the Unix/Linux world, but it has too many command sets, so it is not easy to be proficient in it, except for professional SA, for our developers, we only need to master some of the most common usage. The following describes some common commands:
Command |
Description |
NG |
Jump to line N. N indicates the number of rows. This command immediately redirects the cursor to the specified row. |
G |
Jump to the last line of the file |
Gg |
Jump to the first line of the file |
CTRL + G |
Number of rows and columns at the cursor position |
: Set nu |
Line number displayed in the editor |
CTRL-F |
Flip forward |
CTRL-B |
Flip back |
N + |
Move the cursor down n rows |
N- |
Move n rows above the cursor |
0 |
Move the cursor to the beginning of the current row. Note that the number is zero. |
$ |
Move the cursor to the end of the current row |
Dd |
Delete a row and move it up automatically |
D |
Delete the character from the cursor to the end of the line |
D0 |
Delete the character from the cursor to the beginning of the line. Note that the number is 0. |
D $ |
Delete the character between the cursor and the end of the line |
U |
Undo the last operation, similar to Ctrl + z |
/Pattern |
Search for pattern from the beginning of the cursor to the end of the file |
? Pattern |
Search for pattern from the beginning of the cursor |
N |
Repeat the previous search command in the Same Direction |
N |
Repeat the last search command in the reverse direction |
: S/P1/P2/g |
Replace all p1 in the current row with P2. s represents substitute and g Represents global. |
: N1, N2s/P1/P2/g |
Replace all P1 from line N1 to line N2 with P2. |
: G/P1/S // P2/g |
Replace all p1 in the file with P2. |
: N |
Jump to line N |
I/R |
Change to insert/replace Mode |
Q/Q! |
Exit/force exit without saving |
: W/tmp/tmpfile |
Save the file to the specified file, which is particularly useful when the file is read-only. |
: X/: WQ/ZZ |
Save and exit |