TrumanWong

vi

Powerful plain text editor

Supplementary instructions

vi command is the most versatile full-screen plain text editor in UNIX operating systems and UNIX-like operating systems. The vi editor in Linux is called vim, which is an enhanced version of vi (vi Improved). It is fully compatible with the vi editor and implements many enhanced functions.

The vi editor supports edit mode and command mode. In the edit mode, you can complete the text editing function, and in the command mode, you can complete the file operation commands. To use the vi editor correctly, you must be proficient in switching between the two modes. By default, the vi editor automatically enters command mode after opening it. Use the "esc" key to switch from edit mode to command mode, and use the "A", "a", "O", "o", "I", and "i" keys to switch from command mode to edit mode.

The vi editor provides a wealth of built-in commands. Some built-in commands can be completed using keyboard key combinations, and some built-in commands need to be entered starting with a colon ":". Commonly used built-in commands are as follows:

Ctrl+u: Scroll to the first half of the file;
Ctrl+d: Scroll half-screen to the end of the file;
Ctrl+f: Scroll one screen to the end of the file;
Ctrl+b: Move one screen to the first page of the file;
Esc: Switch from edit mode to command mode;
ZZ: Save the modifications made to the current file in command mode and exit vi;
:Line number: The cursor jumps to the beginning of the specified line;
:$: The cursor jumps to the beginning of the last line;
x or X: delete a character, x deletes the character after the cursor, and X deletes the character before the cursor;
D: Delete all characters from the current cursor to the end of the line where the cursor is;
dd: delete the contents of the cursor line;
ndd: delete the current line and the following n-1 lines;
nyy: Save the contents of the current line and n lines below it to a register? Among them? is a letter, n is a number;
p: Paste text operation, used to paste the contents of the cache area below the current cursor position;
P: Paste text operation, used to paste the contents of the buffer area above the current cursor position;
/String: Text search operation, used to search for the content of the specified string starting from the current cursor position to the end of the file, and the searched string will be highlighted;
? String: text search operation, used to search for the content of the specified string starting from the current cursor position to the head of the file, and the searched string will be highlighted;
a, bs/F/T: Replace text operation, used to replace F string with T string between line a and line b. Among them, "s/" means to perform a replacement operation;
a: Add text after the current character;
A: Add text at the end of the line;
i: Insert text before the current character;
I: Insert text at the beginning of the line;
o: Insert a blank line after the current line;
O: Insert a blank line before the current line;
:wq: In command mode, execute the save and exit operation;
:w: In command mode, perform a save operation;
:w! : In command mode, perform forced save operation;
:q: In command mode, execute the exit vi operation;
:q! : In command mode, perform a forced exit from vi;
:e file name: In command mode, open and edit the file with the specified name;
:n: In command mode, if multiple files are opened at the same time, continue editing the next file;
:f: In command mode, used to display the current file name, the line number of the line where the cursor is located, and the display ratio;
:set number: In command mode, used to display the line number at the far left;
:set nonumber: In command mode, used to not display the line number at the far left;

grammar

vi(options)(parameters)

Options

+<line number>: Display text content starting from the line with the specified line number;
-b: Open the file in binary mode, used for editing binary files and executable files;
-c<command>: After completing the editing task of the first file, execute the given command;
-d: Open the file in diff mode. When multiple files are edited, the differences in the files are displayed;
-l: Use lisp mode, open "lisp" and "showmatch";
-m: Cancel the file writing function and reset the "write" option;
-M: Turn off the modification function;
-n: Disable caching function;
-o<number of files>: Specifies to open the specified number of files at the same time;
-R: Open the file in read-only mode;
-s: Quiet mode, does not display any error messages for the command.

Parameters

File list: Specify the file list to edit. Use spaces to separate multiple files.

Knowledge expansion

The vi editor has three working modes: command mode, input mode and ex escape mode. Through corresponding commands or operations, conversion can be performed between these three working modes.

Command mode

Enter the command vi at the Shell prompt to enter the vi editor and be in vi command mode. At this time, any characters entered from the keyboard are interpreted as editing commands, for example, a (append) represents an append command, i (insert) represents an insert command, x represents a delete character command, etc. If the entered character is not a legal command of vi, the machine will sound an "alarm sound" and the cursor will not move. In addition, the characters entered in command mode (i.e. vi command) are not displayed on the screen. For example, if you enter i, there will be no change on the screen, but by executing the i command, the working mode of the editor will change: by command mode Change to input mode.

Input

You can enter the input mode from the command mode by entering vi's insert command (i), append command (a), open command (o), replace command (s), modify command (c), or replace command (r). In input mode, all characters entered from the keyboard are inserted into the buffer being edited and treated as the text of the file. After entering the input mode, the entered visible characters are displayed on the screen, but the editing commands no longer work and only appear as ordinary letters. For example, if you enter the letter i in command mode, enter the input mode, and then enter i again, a letter i will be added to the corresponding cursor on the screen.

The way to return to the command mode from the input mode is to press the Esc key. If you are already in command mode, pressing the Esc key will make a "beep" sound. In order to ensure that the vi command that the user wants to execute is entered in command mode, you may wish to press the Esc key a few more times and enter the command after hearing the beep.

ex escape method

The functions of vi and ex editors are the same, the main difference between the two is the user interface. In vi, commands are usually single letters, such as a, x, r, etc. In ex, the command is a command line that ends with the Enter; key. vi has a dedicated "escape" command that provides access to many line-oriented ex commands. To use the ex escape mode, enter a colon (:). As with the ex command prompt, the colon appears on the status line (usually the bottom line of the screen). Press the interrupt key (usually the Del key) to terminate the executing command. Most file management commands are executed in ex escape mode (for example, reading a file, writing the contents of the edit buffer to a file, etc.). After the escape command is executed, it automatically returns to the command mode. For example:

:1,$s/I/i/g Press Enter

Then replace all uppercase I's with lowercase i's from the first line of the file to the end of the file ($). The conversion between the three working modes of the vi editor is shown in the figure.

!vi