Detailed introduction of VI commands under LINUX

  
        

vi is short for "Visual Interface", and its status on Linux is as if the Edit program is on DOS. It can perform many text operations such as output, delete, find, replace, block operation, etc., and users can customize it according to their own needs, which is not available in other editing programs.

vi is not a typesetting program. It does not arrange fonts, formatting, paragraphs, and other attributes like Word or WPS. It is just a text editing program.

vi No menus, only commands, and lots of commands. Due to space limitations, this article only describes commonly used commands.

Vi has three basic modes of operation: command line mode, text input mode, and last line mode.

Command Line Mode:

At any time, regardless of the user's mode, just press the "ESC” key to put vi into command line mode; when typing in a shell environment When the vi command starts the vi editor, it is also in this mode.

In this mode, users can enter various legal vi commands to manage their own documents. Any characters entered from the keyboard at this time are interpreted as edit commands. If the input character is a valid vi command, vi completes the corresponding action after accepting the user command (but note that the command entered Not shown on the screen). If the entered character is not a legal command of vi, vi will ring the alarm.

Text input mode:

Enter the insert command i, the additional command a, the open command o, the modify command c, the substitute command r or the replacement command s in the command mode to enter the text input mode. . In this mode, any character entered by the user is saved as a file content by vi and displayed on the screen. In the text input process, if you want to return to the command mode, press the “ESC” button.

Last line mode:

In the command mode, the user presses the “:” key to enter the last line mode, and Vi will be in the last line of the display window (usually also the screen). The last line) shows a “:” as the prompt for the last line mode, waiting for the user to enter the command. Most file management commands are executed in this mode (such as writing the contents of the edit buffer to the file). After the last line command is executed, vi automatically returns to the command mode.

If you change your mind during the input command in the last line mode, you can press the <;ESC” key or use the backspace key to delete all the commands you entered, and then press the backspace key. Vi Go back to command mode.

Entering and exiting vi

In shell mode, type vi and the name of the file to be edited, you can enter vi. For example:

vi example.txt

to edit the example.txt file. If the file exists, the contents of the file will be displayed in the editing interface and the cursor will be positioned in the first line of the file; if the file does not exist, there will be no editing interface content. If you need to place the cursor on the nth line of the file after entering the vi editing interface, add the “+n” parameter to the vi command. For example, if you need to start from the 5th line of the example.txt file, use the following command:

vi +5 example.txt

When you exit vi, you need to enter the exit command in the last line mode. “q”. If in text input mode, first press the “ESC” key to enter the command mode, then enter “ldquo;:” to enter the last line mode in the last line mode, you can use the following exit command:

:q Direct drop out. If you modify the document content in text input mode, you cannot exit.

:wq Exit after saving.

:x with “ wq”.

:q! - Do not save the content, force exit.

Display line number in vi

In the last line mode, enter the following command.

set number

Causes vi to display the line number in the editing interface.

In addition, in the last line mode, you can use the following  nu” command (short for number) to display the line number of the line where the cursor is located and the contents of the line.

Cursor Move Operation

In the full-screen text editor, cursor movement is undoubtedly the most frequently used operation. Users can only use the commands that move the cursor skillfully to quickly and accurately reach the desired location for editing.

The cursor movement in vi can be either in command mode or text input mode, but the method of operation is different.

In text input mode, you can use the four arrow keys on the keyboard to move the cursor directly; in command mode, there are many ways to move the cursor. Not only can you use the four arrow keys to move the cursor, but you can also use the four keys h, j, k, l instead of the four arrow keys to move the cursor. This avoids the contradiction caused by different keyboard definitions on different machines. And after using the proficient, you can do all the work without leaving the letter keyboard position, thus improving work efficiency.

The following commands all perform cursor movement in command line mode:

h The cursor moves to the left. If you enter the number n before pressing the h command, the cursor moves left by n characters;

l The cursor moves to the right. If you enter the number n before pressing the l command, the cursor moves to the right by n characters;

j The cursor moves up, if you enter the number n before pressing the j command, the cursor is on Move n characters;

k Move the cursor down. If you enter the number n before pressing the k command, the cursor xia is shifted by n characters;

0 (zero) The cursor moves to the beginning of the line;

$ The cursor moves to the end of the line;

H The cursor moves to the first line displayed on the screen (not necessarily the file header);

L The cursor moves to the screen The last line shown (not necessarily the end of the file);

M The cursor moves to the middle of the screen;

nG The cursor moves to the nth line;

w Or W move the cursor right to the beginning of the next word;

e or E If the cursor starts at a position within the word (ie, at the end of the word), the command moves the cursor to the end of the word ; if the cursor starts at the end of the word, This command will move the cursor to the next word ending.

b or B If the cursor is within the word (ie, not the first word), the command will move the cursor to the beginning of the word; if the cursor is at the beginning of the word, the command will place the cursor Move to the beginning of the previous word;

Ctrl+G Status command, showing the status of the currently edited document. Includes the name of the file being edited, whether it has been modified, the current line number, the number of lines in the file, and the percentage of the line before the cursor as a percentage of the entire file.

In addition, you can also use the following command in the last line mode to complete the

movement of the cursor in the entire file:

:n Move the cursor to the nth line of the file ;

:$ The cursor moves to the last line of the file;

Text insertion operation

In command mode, any character entered by the user is interpreted as a command by vi Execution, if the user wants to treat the input character as text content, first switch the working mode of vi from command mode to text input mode. The way to switch is to use the following command:

1. Insert command

vi provides two insert commands: i and I.

(1) . The i command inserts text from the front of the cursor position, and the wrong input can be deleted using the key during insertion. At this time, vi is in the inserted state, and the bottom line of the screen displays “-INSERT--” Insert the typeface.

(2) . I Command This command moves the cursor to the beginning of the current line and then inserts the text before it.

2. The append command

vi provides two additional insert commands: a and A.

(1) . a command This command is used to append new text after the current position of the cursor. The newly entered text is placed after the cursor, and the original text after the cursor will move backward accordingly. The cursor can be Anywhere in a row.

Copyright © Windows knowledge All Rights Reserved