The bottom editor used by the VI editor is the ex line editor, and you can assume that the VI editor is the visual mode of the ex line editor. Ex can give you more mobile, more powerful editing commands. EX is behavioral-oriented and has the advantage of making large-scale changes to files.
Open File:
The "ex filename" command opens the file using the ex line editor, which lists the total number of files and the command prompt (:), where you cannot see any line of the file, unless you release an ex command that displays one or more lines, as shown in:
The ex command consists of the line address (which can be just a line number) and the corresponding command. The most basic command is P, which represents printing (to the screen), so entering "1p" at the command prompt means printing the first line. You can even omit p at this point, and the simple input line number is equivalent to displaying the row. Specify the line number of a range by separating the two values with a comma, for example, "1,3" for lines 1th through 3rd. a command that has no line number will only work on the current row.
Enter the ": VI" command to enter the VI editor from the ex editor, or enter "Q" in the VI editor to enter the ex editor. To use the EX Editor command in the VI editor, you must enter a specific character: (colon). By entering the line number after the colon, you can jump directly to the line , for example ": 6" is equivalent to the "6G" command in the VI editor, moving the cursor to line 6th.
Edit with ex:
Many of the ex commands responsible for common editing operations have a simpler command in VI. However, the ex command can be more useful if you need to change many rows. The common ex commands are:
Full name abbreviation feature
Delete D Deletes a row
Move M moves the line
Copy CO replication line
T copy rows
Line Address: every ex edit command needs to know the line number to edit. There are several ways to specify a line address:
(1) The specified line number is clear;
(2) Use the symbol to specify the line number relative to the current position;
(3) Identify certain lines of the search mode as the address;
Define line ranges: the range of a row or segment of a row is clearly defined by the line number. an unambiguous specified line number is also known as an absolute line address. For example
: 3,18d Delete 3 to 18 rows;
: 160,224m23 moves line 160th to line No. 244 after line 23rd;
: 23,29co100 the line 23rd through 29th to line 100th;
by: Set NU or: Set number to display line numbers, by: Set Nonu or: Set Nonumber to turn off the display of line numbers. You can temporarily display the line number of some rows by using #, for example, ": 1,10#" to temporarily display line numbers for lines 1th through 10th. You can also get the line number information by using the following command:
: = List The total number of files;
:. = Lists the line number of the current line;
:/pattern/= lists the line numbers when the pattern first appears;
line addressing symbols: you can use symbols to represent line addresses, which can be combined with an absolute row address.
. Represents the current row;
$ represents the last line of the file;
% represents each line of the file, equivalent to a combination of "1,$";
For example " :., $ D " deletes the text between the current line and the end of the file. " : 20,. M $ " moves the file contents of line 20th to the current line to the end of the file. " :%d" deletes all rows of the file. " :%t$" copies all rows to the end of the file (for continuous replication).
In addition to specifying an absolute row address, you can also specify the address relative to the current line. + And-Represents the addition or subtraction of the following values, such as " :., +20d" to delete the file contents of the current line to 20 rows after the line. " : 226, $ m-2" moves the file contents of line No. 226 to the end of the file before the two lines of the current line. The number 0 represents the beginning of the file, which is before the first line of text, so 0 is equivalent to 1-.
Search mode: You can also specify a line address using the search mode. For example
:/pattern/d Delete the next line containing the pattern;
:/pattern/+d Deletes the next row of the next line containing the pattern;
:/pattern1/,/pattern2/d Delete from the first row containing pattern1 to the first row containing the pattern2;
Before and after these patterns use a slash as the dividing line.
redefine the location of the current line: When a semicolon is used in place of a comma, the first line address will be the current line address, even if the cursor is not on that line at this time. For example, " : 100;+5 P" Displays the contents of line 100th and its next 5 rows.
Global Search:
There is a global command G in ex that can search for patterns and display all rows that contain this pattern. and ": g! "is exactly the same as": G ", searching for all rows that do not contain the specified pattern.
: G/pattern find (move to) the place where the last pattern of the file appears;
: g/pattern/p Find and display all lines containing the pattern in the file;
: G!/pattern/nu finds and displays all lines in the file that do not contain the pattern, and displays the line numbers of all rows found;
: 60,124g/pattern/p finds and displays the line that contains the pattern between line 60th and line 124th.
Merge ex command:
To enter a new ex command, you do not need to enter a colon every time, you can use the vertical bar (|) to separate the commands, and then merge multiple commands at the same ex prompt.
Save and leave the file:
: W writes the contents of the buffer to the file, but does not leave. ": w" should be used frequently when editing files;
: Q Leave the editor;
: Wq writes the file while leaving the editor, which is unconditionally written, even if the file has not been modified;
: X writes the file and leaves the editor. Only files are written when they are modified;
This chapter VI command summarizes:
ex,1p,1,2p,d,m,co,t,:=,:.=,:/pattern/=,.,$,%, +,-,:/pattern/d,:g,:g!, |, : W, : Q,
: Wq, : x
Learning VI and VIM editor (6): Ex Editor overview (1)