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. Below is a summary of the vi usage Tutorial: 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.
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, MS/old/new/g // replace all old 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 !!!
The original Article must be reproduced with the author's consent, and the source must be indicated in the form of a link.
> Reprinted please indicate the source: VPS detective link address: http://www.vpser.net/manage/vi.html