Vi is a full-screen editor working in the character terminal environment. Therefore, the editing interface is relatively simple and does not provide users with mouse operations and menu systems, the editing and operation functions are implemented by pressing commands. Three working modes can be used on the Vi editing interface: Command mode, input mode, and last line mode, operations on files vary in different modes.
Command mode: After the vi editor is started, the command mode is enabled by default. In this mode, operations such as moving the cursor, searching strings, and deleting, copying, and pasting file content are completed.
Input mode: the main operation in this mode is to enter the file content. You can modify the text of the text file or add new content. When entering the input mode, the last line of the vi editor will display the "-INSERT --" status prompt.
Last Row mode: This mode allows you to set the vi editing environment, save the file, exit the editor, and perform operations such as searching and replacing the file content. The last line of the Vi editor displays ":" prompt.
Command mode, input mode, and last line mode are three States of the vi editing environment. You can switch between different modes by pressing different buttons, for example, by pressing the colon ": "The key can enter the last line mode. If you press a, I, o, and other keys, you can enter the input mode. In the input mode and last line mode, Press ESC to return to the command mode.
Command operation'
Run the independent "vi" command to enter the command mode of the vi editor. The related version information is displayed.
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/21121323G-0.jpg "title =" 2.JPG"/>
650) this. width = 650; "src =" http://img1.51cto.com/attachment/201306/184245764.jpg "title =" 1.JPG"/>
When learning the basic operations of the vi Editor, we recommend that you copy a system configuration file with more content for practice. Instead of directly modifying system files, you can avoid System Faults due to errors.
For example, copy the/etc/inittab file in the system to the current directory and use the vi editor to open the copied new file.
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/211213CX-2.jpg "title =" 4.JPG"/>
Mode switch
In command mode, you can use the, I, or o buttons to quickly switch to the input mode. At the same time, you can determine the method and position of the insertion point to input the file content. When you need to return the command mode, press ESC,
A: insert content after the current cursor position
A: insert content at the end of the row where the cursor is located.
I: insert content before the current cursor position
I: insert content at the beginning of the line where the cursor is located.
O: Insert a new line behind the row where the cursor is located.
O: Insert a new row in front of the row where the cursor is located.
Move cursor
1. move the cursor in the direction: Use the four arrow keys in the keyboard to move the cursor.
2. Page flip: Use the Page Down or shortcut key Ctrl + F to scroll Down the entire Page.
3. Use the Page UP or shortcut key Ctrl + B to flip the entire Page
Fast intra-row jump
Press the Home key or the "^", number "0" key to quickly jump the cursor to the beginning of the row.
Press the End key or the "$" key to quickly jump the cursor to the End of the row.
Fast jump between lines
Use the 1g or gg key command to jump to the 1st line of the file content.
Press the key command G to jump to the last line of the file.
Use the key command # G to jump to the # Line in the file, where "#" is replaced by a specific number ).
To view the inter-line jump effect, you can learn how to display the row number in the vi editor. You only need to switch to the last line mode and execute the ": set nu" command to display the row number,
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/2112133227-3.jpg "title =" 5.jpg"/>
For example, run the ": set nonu" command to undisplay the row number.
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/2112133W3-4.jpg "title =" 6.jpg"/>
Copy, paste, and delete
1. Delete: Use the x or del key to delete a single string at the cursor.
Run the "dd" command to delete the row where the current cursor is located. Use the "# dd" command to delete the # Line Content starting from the cursor #: Replace the specific number)
Run d ^ to delete all the characters from the cursor to the beginning of the line.
Press the d $ button to delete all characters from the current cursor to the end of the line.
2. assign values. Run the key command yy to copy the entire line of the current line to the clipboard, in the form of # yy, you can also copy the # Line Content starting from the cursor. "#" indicates that the number of the graph is replaced ). The copied content must be pasted before it can be used.
3. paste operation. In the vi editor, the previous deleted or copied content will be saved to the clipboard buffer. You only need to press the p key to paste the content in the buffer to the cursor position, press the p key and paste it before the cursor is displayed.
4. File Content Search
In command mode, press the "/" key and enter the specified string to start searching backward from the current cursor. If you press "?", Key ). After the search is complete, you can press the n and N keys to select different search results.
For example, enter/initdefault and press enter to search for the "inindefault" string in the file and highlight the result. The cursor automatically moves to the first search result and press the n key to move to the next search result.
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/2112136213-5.jpg "title =" 7.jpg"/>
5. unwrite, save, and exit
When editing the file content, you sometimes need to cancel some incorrect editing operations. you can press the "u" U "Key, where, the u key command is used to cancel the last operation and restore the operation result. You can repeatedly press the u key to reply to the ongoing multi-step operation; the U key command is used to cancel all edits made to the current user.
When you need to save the current file content and exit the vi editor, you can press the ZZ command.
6. Basic operations in the last row Mode
In command mode, press the colon ":" to switch to the last line mode. The ":" prompt is displayed in the last line of the vi editor. You can enter a specific last line command at the prompt, complete functions such as saving files, exiting the editor, opening new files, reading other file content, and replacing strings.
1) save the file and exit the vi editor.
1. Save the file. After modifying and confirming the file content, run the w command to save the file content.
: W
To save it as another file, you need to create a new file name. If necessary, you can specify the file path.
Example: w/root/newfile
Exit the editor. to exit the vi Editor, Run ": q". If the file content has been modified but is not saved, the ": q" command cannot exit successfully, you need to use ": q! "Command Force Exit: exit without saving ).
Save and exit. You can use the ": wq" or ": x" command to save the file and exit the editor. : Wq
This article is from the "Network System Maintenance" blog. For more information, contact the author!