I. Introduction
VI is the most common full-screen editor in the Unix world. in Linux, VIM is the enhanced VI version. Vim is fully compatible with vi. VI is the abbreviation of "Visual Interface. It can perform output, delete, search, replace, block operations, and many other text operations, and users can customize it according to their own needs, which is not available in other editing programs.
VI is not a typographical program. Unlike word or Wps, it can orchestrate fonts, formats, paragraphs, and other attributes. It is just a text editing program.
VI has no menu, only commands, and many commands. You can enter the VI editing environment by typing VI on the command line. VI has three statuses: Command mode, text input mode, and last line mode. Enter VI and press enter to enter the command mode of VI.
In command mode, any character entered by the user is interpreted and executed by VI as a command. If the user wants to treat the entered character as text content, switch the VI operating mode from command mode to text input mode. In command mode, press the "a" ("A") or "I" ("I") or "O" ("O") key, enter the inserted text input mode. Press the "A" key to insert to the end of the current cursor; press the "I" key to insert at the current cursor; press the "A" key to insert at the end of the row where the current cursor is located; press the "I" key to insert at the beginning of the row where the current cursor is located. In text input mode, Press ESC to return to command mode.
The last row mode is also called the ex escape mode. VI has a dedicated "escape" command to access many line-oriented ex commands. In command mode, you can press the ":" key to enter the last line mode. At this time, VI will display a ": as the last line prompt, waiting for the user to enter the command. Most file management commands are executed in this mode (for example, writing the content in the editing buffer to a file is moderate ). After the last-line command is executed, VI automatically returns to the command mode.
For example:
: 1, $ S/A/G
Replace uppercase A with lowercase A from the first line to the end of the file.
If you want to switch to the command mode when you enter the command in the last line mode, press the button or use the return key to delete all input commands, and then press the return key, the VI can be returned to the command mode.
To switch from command mode to edit mode, TYPE command a or I. To return from text mode, Press ESC. In command mode, enter ":" to switch to the last line mode, and then enter the command.
Many Commands in VI use numeric values such as row number and row number. If the edited file is large, it is inconvenient to calculate it by yourself. Therefore, VI provides the function of adding row numbers to texts. These row numbers are displayed on the left of the screen, and the content of the corresponding row is displayed after the row number. It should be noted that the row numbers added here are only displayed to users, and they are not part of the file content.
When you use VI to create a new file, you can leave no file name in the VI command. After editing the file, you must specify the file name to save the data.
When entering Vi, you can not only specify a file name to be edited, but also have many additional operations. If you want to move the cursor to a specific row in the file after entering VI, you can add the option + N after Vi, where N is the specified number of rows. For example, after you enter the command vi + 3 example.txt, the optical mark will be placed on line 3rd in example.txt.
Ii. Common VI commands
1. Insert command
VI provides two insert commands: I and I.
(1) I command
The inserted text starts before the cursor position, and you can use the key to delete incorrect input during the insertion process. At this time, the VI is in the Insert state, and the "-- insert --" (insert) is displayed at the bottom of the screen.
Example 1: A file being edited is as follows:
Welcome to VI world! Come on!
~
~
The cursor is located at the first "!" To insert the following information to the front:
This is an example!
Run the I command and enter the corresponding text. The screen is displayed as follows:
Welcome to VI world this is an example !! Come on!
~
~
In this example, we can see that the cursor is originally in the first "!" But it is inserted before the cursor position, so this "!" It is squeezed into the new inserted text.
(2) I command
This command is to move the cursor to the beginning of the current line, and then insert text before it.
2. move the cursor on the screen
In the full-screen text editor, moving the cursor is undoubtedly the most frequently used operation. Users can quickly and accurately edit the cursor at the desired position only by skillfully using these commands.
The cursor movement in VI can be in command mode or text input mode, but the operation methods are different.
(1) In text input mode, you can use the four arrow keys on the keyboard to move the cursor.
(2) In command mode, there are many ways to move the cursor. Not only can we use four direction keys to move the cursor, but we can also use the H, J, K, and l keys to move the cursor instead, this can avoid conflicts caused by different keyboard definitions on different machines, and you can complete all operations without leaving the letter and keyboard location after you are proficient, thus improving work efficiency.
3. Move on the screen
VI provides three commands for moving the cursor across the screen without scrolling the file itself. They are h, M, and l commands.
(1) h command
This command moves the cursor to the first line of the screen (that is, the upper left corner), that is, the first line of the current screen, rather than the first line of the entire file. You can use this command to quickly move the cursor to the top of the screen. If n is added before the H command, move the cursor to the beginning of the N line. It is worth mentioning that the command DH will delete all content from the current row of the cursor to the first line of the displayed screen.
(2) m command
This command moves the cursor to the beginning of the line in the middle of the display file. That is, if the current screen is full, it will be moved to the middle line of the screen; if it is not full, it will be moved to the middle line of the lines of the text. This command can be used to quickly move the cursor from any position on the screen to the beginning of the line in the middle of the screen display file. For example, when the above screen is displayed (no matter where the cursor is on the screen), in command mode, after the command m is input, the optical mark will be moved to the letter A in the Add line. It is also worth mentioning that using the command DM will delete all content from the current row of the cursor to the middle row of the displayed file on the screen.
(3) l command
When the content displayed in the file exceeds one screen, this command moves the cursor to the beginning of the line at the bottom of the screen. When the content displayed in the file is insufficient, this command moves the cursor to the beginning of the last line of the file. This command can be used to quickly and accurately move the cursor to the bottom of the screen or the last line of the file. If n is added before the l command, move the cursor to the beginning of the N line starting from the bottom of the screen. It is also worth mentioning that the command DL will delete all content from the current row of the cursor to the bottom line of the screen.
4. Edit commands
Let's take a look at the simplest and most common editing process: No one makes mistakes. So sooner or later you will encounter the modification of the text you entered. In fact, most of the time spent on word processing is modification rather than entering new things. Therefore, it is important to know how to easily modify it.
(1) x -- delete a character
The simplest way to delete text is to use X. The result of this command is that the character of the cursor disappears and the text behind it moves left. If the character you delete is the last character of a line, the cursor will move to the left so that the cursor will not stay below the nonexistent character. If there is no text, the speaker is called.
(2) d -- delete an object
The right side of this command also has certain text objects. A text object is a piece of text. On the right side is the characters that control the movement of the cursor. For example, if W indicates the previous word, DW deletes the next word. 5 W indicates the forward five words, then the D5W will delete them.
(3) dd -- delete a row
One of the most common D-series commands. As before, 5dd will delete 5 rows
(4) d -- entire Deletion
The uppercase D is used to delete the cursor from the cursor to the end of the row. Same effect as d $.
(5) U -- Recovery
Regret it? It does not only undo the deletion, but also undo all your edits.
(6). -- duplicate
Repeat the edits.
5. Exit vi
You can use one of the following methods to exit VI and return to shell after editing the file.
(1) In command mode, the uppercase letter Z is used for two times. If the current edited file has been modified, VI will save the file and exit, and return to shell; if the current edited file has not been modified, VI exits directly and returns to shell.
(2) In the last line mode, run the following command: w vi to save the current edited file, but does not exit. Instead, it continues waiting for the user to enter the command. When you use the W command, you can create a new file name for the editing file.
Example: W newfile
At this time, VI will save the content of the current file to the specified newfile, while the original file remains unchanged. If newfile is an existing file, the VI prompts in the status line of the display window:
File exists (use! To override)
If you really want to replace the original content in newfile with the current content of the file, you can use the command
: W! Newfile
Otherwise, you can select another file name to save the current file.
(3) In the last line mode, enter the command: Q. Exit VI and return to shell. If the editing file is not saved when you exit VI with this command, the last line of the VI display window displays the following information:
No write since last change (use! To overrides)
The user is prompted that the file is not saved after modification, and then VI does not exit, continue waiting for the USER command. If you want to force exit VI without saving the modified file, run the following command: Q! VI abandons the modification and directly returns it to shell.
(4) In the last line mode, enter the command: wq vi will save the file first, and then exit VI and return to shell.
(5) In the last line mode, the input command has the same function as the ZZ command in the command mode.