VI (Vim) is a commonly used editor on Linux. By default, VI (Vim) is installed in many Linux distributions ). There are many VI (Vim) commands, but if they are used flexibly, the efficiency will be greatly improved. VI is short for "Visual Interface", and VIM is VI improved (enhanced version VI ). In general system management and maintenance, VI is enough. If you want to use code to highlight it, you can use vim. The following describes how to use VI: including the basic introduction of VI, usage mode, opening, closing, saving, inserting text or creating a row, moving the cursor, deleting, restoring characters or rows, searching, etc, this is a more suitable tutorial for beginners to learn VI.
VI has three modes: insert mode, command mode, and low-line mode.
Insert mode: You can enter characters in this mode. Press ESC to return to the command mode.
Command mode: you can move the cursor or delete characters.
Low-line mode: You can save files, exit VI, set VI, search, and other functions (low-line mode can also be seen as in command mode ).
1. Open, save, and close a file (used in VI command mode)
VI filename // open the filename File
: W // save the file
: W vpser.net // save it to the vpser.net File
: Q // exit the editor. If the file has been modified, run the following command:
: Q! // Exit the editor and do not save
: WQ // exit the editor and save the file
2. Insert text or line (in VI command mode, run the following command to enter the insert mode. Press ESC to exit the insert mode)
A // Add text to the right of the current cursor position
I // Add text to the left of the current cursor position
A // Add text at the end of the current row
I // Add text at the beginning of the current line (the beginning of a non-null character)
O // create a new row above the current row
O // create a new row under the current row
R // Replace (overwrite) The current cursor position and subsequent text
J // merge the row where the cursor is located and the next row as one line (still in command mode)
3. move the cursor (used in VI command mode)
1. Use the up, down, and left direction keys
2. In command mode, H goes to the left, J goes down, K goes up, and l goes to the right.
Space key to the right, backspace to the left, enter to the next line first,-to move to the previous line first.
4. delete or restore characters or lines (used in VI command mode)
X // Delete the current character
NX // Delete n characters starting from the cursor
Dd // Delete the current row
NDD // Delete n rows including the current row down
U // undo the previous operation
U // undo all operations on the current row
5. Search (used in VI command mode)
/Vpser // search for a vpser string under the cursor
? Vpser // search for a vpser string from the cursor
N // search down the previous search action
N // search for the previous search action
6. Jump to a specified line (used in VI command mode)
N + // jump n rows down
N-// jump n rows up
Ng // jump to the row whose row number is n
G // jump to the bottom of the file
7. Set the row number (used in VI command mode)
: Set nu // display the row number
: Set Nonu // undisplay the row number
8. copy and paste (used in VI command mode)
YY // copy the current row to the cache, or use "Ayy copy," A as the buffer, and a can be replaced with any letter from A to Z, you can complete multiple replication tasks.
Nyy // copy the N rows of the current row to the buffer. You can also use "anyy replication," A is the buffer, and a can also be replaced with any letter from A to Z, you can complete multiple replication tasks.
YW // copy the characters starting from the cursor to the end of the word.
Nyw // copy the N words starting from the cursor.
Y ^ // copy the content from the cursor to the beginning of the line. VPs detective
Y $ // copy the content from the cursor to the end of the row.
P // paste the content in the clipboard after the cursor. If the preceding custom buffer is used, we recommend that you use "AP" for pasting.
P // paste the content in the clipboard before the cursor. If you use the custom buffer above, we recommend that you use "AP for pasting.
IX. Replacement (used in VI command mode)
: S/old/new // replace the old
: S/old/new/g // replace all the old in the row with new
: N, m s/old/new/g // replace all the old values from N to m with new
: % S/old/new/g // replace all old files in the current file with new
10. Edit other files
: E otherfilename // edit the file named otherfilename.
11. Modify the file format
: Set fileformat = UNIX // modify the file to Unix format. For example, ^ m will appear in the text file under Windows in Linux.
Summary: Although there are many commands in the VI (Vim) tutorial, as long as you work diligently and practice, I believe you will be very familiar with it soon, at the same time, it will bring higher efficiency to your work or study. If you do not know what mode you are in, press the ESC key twice to return to the command mode. A last note will be given: Pay attention to Case sensitivity !!!