UNIX linux vi command 1. vi file editing commands have special characteristics, including editing mode and command mode (inconsistent with unix commands, such as ll and who). Therefore, when editing files under Vi commands, it is often necessary to switch various command modes within the system. For example, you cannot delete commands during normal editing. You can only enter commands. You can delete or insert commands only when you switch to command mode. To exit the save operation, you must: wq !, Can be saved. The command to exit or not save is special. It consists of multiple characters and starts with a colon (last line mode ). The vi editor has three modes: Command mode, input mode, and last line mode. It is very important to master these three modes: The command mode is entered by default after vi is started. From this mode, you can switch to the other two modes by using commands, in any mode, you can press the [Esc] key to return to the command mode. Enter the subtitle "I" in command mode to enter the vi input mode to edit the file. Input mode: In this mode, you can edit, modify, and input files. In the last line of the editor, "-- INSERT --" is displayed, indicating that vi enters the input mode. When modifying input and other operations, we need to save the file. At this time, we need to first return to the command mode and save it in the last line mode. Last line mode: Enter ":" In command mode to enter this mode. There are many useful commands in last line mode. 1. run vi filename: open or create a file, place the cursor at the beginning of the first line vi + n filename: open the file, and place the cursor at the beginning of line n vi + filename: open the file, place the cursor at the beginning of the last line vi +/pattern filename: open the file, and place the cursor at the first string matching pattern vi-r filename: a system crash occurred when editing with vi last time. Restore filenamevi filename .... filename: open multiple files and edit them one by one. move the cursor command h or Backspace: move the cursor to the left one character l or space: move the cursor to the right one character k or Ctrl + p: move the cursor up one line j or Ctrl + n or Enter: move the cursor down a row w or W: move one word to the right of the cursor to B or B: move one word to the left of the cursor to e or E: move one word to the right to the end of the word): move the cursor to the end of the sentence (: move the cursor to the beginning of the paragraph}: move the cursor to the beginning of the paragraph {: move the cursor to the end of the paragraph n G: move the cursor to the first n rows +: move the cursor down n rows n-: move the cursor up n rows n $: move the cursor to the end of n rows H: move the cursor to the top line of the screen M: move the cursor to the middle line of the screen L: move the cursor to the last line of the screen 0: (Note that the number is zero) move the cursor to the beginning of the current line $: move the cursor to the end of the current line 3. screen tumble command Ctrl + u: Ctrl + d: Ctrl + f: Ctrl + B; flip a screen at the beginning of the file nz: roll line n to the top of the screen. If n is not specified, roll the current line to the top of the screen. 4. insert text commands (text input mode) I: Before the cursor I: at the beginning of the current line a: After the cursor A: at the end of the current line o: open a new line under the current line O: open a new line above the current line r: Replace the current character R: Replace the current character and the character behind it until Press ESC key s: Start from the current cursor position, replace the specified number of characters with the input text S: Delete the specified number of rows, and replace ncw with the input text or nCW: Modify the specified number of characters nCC: modifies a specified number of rows. DELETE command ndw or ndW: Delete the n-1 words at the beginning of the cursor and then do: Delete to the beginning of the line d $: Delete to the end of the line ndd: Delete the current line and the next n-1 line dd: delete A Line x or X: delete a character, x deletes the line after the cursor, and X deletes the Ctrl + u before the cursor: Delete the text entered in the input mode 6. copy the entire line to the vi buffer yw, and copy the content from the current cursor position to the end of the word to the vi cache, it is equivalent to copying the word y $ from the cursor position to the end of the line to the cache area y ^ copying the cursor position to the beginning of the line to the cache area # yy For example: 5y Y is to copy 5 rows # yw For example: 2yw is to copy two words. If you want to copy the content between the m row and the n row, you can enter m in the last row mode, for example: 3. 5y copies the content from the third row to the fifth row to the cache area. Paste the content in the buffer, and use p 7. undo operation u command to cancel the last operation, you can use multiple times to restore the original Operation U cancel all operations Ctrl + R can restore the operation using the u command 8. the search and replace command vi's search and replace functions are mainly completed in the last line mode: top-down lookup/character seek to be searched, where/indicates searching starts from the cursor position, for example, what is the search result from/work to bottom? For example, replace "s/old/new" with "new" to replace the first occurrence of "old" in the line: s/old/new/g replace all existing old :#, # s/old/new/g replace the old in line # To line # With new: % s/old/new/g replace the old 9. option settings: set nu show row number: set nonu hide row number: set showmode show current operation mode (if vedit will automatically open): set noshowmode close Mode show: set show all vi variables: set all display all possible vi variables and their current values all: list all options set Condition term: set terminal type ignorance: Ignore case list in Search: show the tab (Ctrl + I) and the line tail sign ($) number: display the row number report: display the number of lines modified by the line-oriented command terse: display the brief warning information warn: When you go to another file, if the current file is not saved, NO write information is displayed. nomagic: allows you to use the special character nowrapscan without "\" in search mode: prohibit vi from accessing the two ends of the file and start mesg from the other end: Allow vi to display the information written by other users to their terminal by write 10. last line command (last line mode): n1, n2 co n3: copy the content from line n1 to line n2 to line n3: n1, n2 m n3: move the content from line n1 to line n2 to line n3: n1, line n2 d: Delete the content from line n1 to line n2: w: Save the current file: e filename: open File filename and edit: x: Save the current file and exit: q: Exit vi: q!: Do not save the file and exit vi :! Command: execute shell command: n1, n2 w! Command: The content from line n1 to line n2 in the file is used as the command input and executed. If n1 and n2 are not specified, the entire file content is used as the command input: r! Command: place the output result of the command to the current line 11. When editing a file, the recovery file vi will generate a temporary file that starts with. And ends with. swp. Normally exit the file and automatically delete it. If the file is unexpectedly exited, such as a sudden power failure, the file will not be deleted. We can choose the command next time when editing: O read-only open, do not change file content E continue to edit the file, do not restore. the content stored in the swp file R will restore the content of the file not saved after the last edit Q exit vi D Delete. swp file or use the vi-r file name to restore unsaved content 12. register operation "? Nyy: Save the content of the current row and Its n rows to the register?, Where? It is a letter, and n is a number "? Nyw: Save the current row and Its n characters to the register?, Where? It is a letter, and n is a number "? Nyl: Save the current row and Its n characters to the register?, Where? It is a letter, and n is a number "? P: retrieve the register? And place it at the cursor position. Here? It can be a letter or a number ndd: delete n lines of text in the current row and its bottom, and put the deleted content in the delete register 1.