In-depth analysis of VI editor usage in Linux 1. vi editor uses vi as a standard Linux text editing program, instead of window-based www.2cto.com multi-purpose editing program, which can be used on various terminals to edit a variety of files and multiple systems, for example, 1.1 vi finename can be used on Solaris, AIX, HP-UNIX, RedHat, and other operating systems. If filename already exists, vi will open the existing file. If filename does not exist (not created yet ), vi creates a file named filename. If filename contains a path, filename is differentiated from the current path and relative path. If it does not contain a path, the file is created under the current path. 1.2 three modes of vi 1.2.1 input mode: When you run the vi editor by entering the text (edit mode), the command mode is usually first entered. You can enter the following command to enable the editor to enter the input mode: www.2cto.com I input I in command mode. you can insert text at the beginning of the line. I input I in command mode, you can insert text A to the left of the cursor. Input A can add text a to the end of a line. Input A can insert text to the right of the cursor. O open A line above the current cursor and prompt for input. o open a line below the current cursor position and prompt u to undo the previous command: enter u immediately after the last command to undo the command, for example, undo the input. repeat a command U to use U to cancel all your changes to a row. Note: only when you have not moved the cursor to a location other than the row you edited, you can use the U command to undo this operation: If you want to undo this operation after entering a line in vi input mode, please keep the cursor in this line after entering (do not move outside this line and then move back ), Then press Esc to switch the input mode of the vi editor to the command mode, and then press U to cancel the input of the row. 1.2.2 command mode: run the command. When the vi editor is in input mode, press the Esc key on the keyboard to Enter command mode. In command mode, enter commands to execute various functions, most vi commands consist of one or two letters plus an optional number. The following command starts with a colon: w + press enter to save without exiting: wq + press enter to save and exit: x + press enter to save and exit (mandatory): q! + Press enter to save and exit: q! + Press enter to exit without saving (force). In non-input mode (edit mode), how to move the cursor: h: Move a character www.2cto.com l to the left: Move a character j to the right: moving a row down k: It is generally not recommended to move the four keys of a row up. The top, bottom, left, and right arrows on the keyboard are recommended ^ :( Shift + 6) move to the beginning of the current row $ :( Shift + 4) Move to the end of the current row H: move the cursor to the top of the screen M: move the cursor to the center of the screen L: move the cursor to the bottom of the screen G: move the cursor to the end of the file Page Up: Flip Up one Page Down: Flip Down half screen x: delete a character dd: delete a line of ndd: (n is the number of rows) delete n rows yy: copy a row p: paste x/dd/ndd + p: move text content: Delete the content to be moved first using x/dd/ndd, then press p and paste it: r filename to copy the content of the specified file filename. Method: Enter r filename + press enter at the place where the text is to be inserted (cursor position. /: Query string usage: Enter/and enter the string to be searched after, then press enter to search for the input string/str_to_find + press Enter n to jump to the next occurrence of the string enter N to jump to the previous occurrence of the string www.2cto.com advanced usage: Replace the string: s/old/new Replace the string old in the first line with the new string new: s/old/new/g, replace all the strings old in the same line with the new string new :#, # s/old/new/g replace all strings old in the two rows with the new string new: % s/old/new/g, replace all strings old in the file with the new string new: when % s/old/new/gc is added with the c option, you can ask the user to confirm each replacement operation setting vi when performing the above: % s/old/new/g operation: set nu set line mark (row number): set nonu cancel line number display: set showmode set Display User Mode: set readonly set file read-only last line mode: execute specific commands