Vi and vim Editor (6): ex editor Overview (1), vimex
The bottom-layer editor used by the vi editor is the ex-row Editor, which can be considered as the visual mode of the ex-row editor. Ex allows you to edit commands with higher mobility and capabilities. Ex is behavior-oriented and has become an advantage when making large-scale changes to files.
Open the file:
The "ex filename" command uses the ex-line editor to open the file. It lists the total number of lines of the file and the command prompt (:). At this time, no line of the file can be seen, unless an ex command is issued to display 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 indicates printing (to the screen). Therefore, input "1 p" in the command prompt to print the first line. In this case, p can be omitted. Simply entering a row number is equivalent to displaying the row. You can use commas (,) to separate two values to specify the row number of a range. For example, "1st" indicates rows 3rd. A command without a line number only takes effect on the current line.
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 ). Enter the row number after the colon to jump directly to the row. For example, ": 6" is equivalent to the "6G" command in the vi editor. All the commands move the cursor to line 6th.
Edit with ex:
Many ex commands in charge of common editing operations have corresponding and simpler commands in vi. However, if you need to change many rows, the ex command may be more useful. Common ex Commands include:
Full name abbreviations
Delete d delete a row
Move m move row
Copy co copy row
T copy row
Line address:The row number to be edited is required for each ex edit command. You can use the following methods to specify a row address:
(1) specify a specific row number;
(2) Use Symbols to specify the row number relative to the current position;
(3) Identify the search mode of certain rows as the address;
Define the row range:The range of a row or segment is clearly defined by the row number. The specified line number is also called the absolute line address. For example
: 3, 18d delete 3 to 18 rows;
: 160,224 m23 moves 160th rows to 244th rows after 23rd rows;
: 23, 29co100: Copy 23rd to 29th rows to 100th rows;
Use: set nu or: set number to display the row number. Use: set nonu or: set nonumber to disable the display of the row number. You can use # To temporarily display the row numbers of some rows. For example, ": 1st #" can temporarily display the row numbers of rows 10th. In addition, you can obtain the row number through the following command:
: = List the total number of objects;
:. = Lists the row numbers of the current row;
:/Pattern/= lists the line numbers when pattern appears for the first time;
Line addressing symbol:A line address can be expressed by a symbol. These line addressing symbols can be used together with an absolute line address.
. Indicates the current row;
$ Indicates the last row of the file;
% Indicates each row of the file, equivalent to a combination of "1, $;
For example, ".., $ d" deletes the text from the current row to the end of the file. ": 20,. m $" Move the file content from row 20th to the current row to the end of the file. ": % D" delete all rows of the file. ": % T $" Copy all rows to the end of the file (continuous copy ).
In addition to specifying the absolute line address, you can also specify the address relative to the current line. + And-Indicates adding or subtracting the following values, for example, ": .., + 20d". delete the file content of the current row to the last 20 rows after the row. ": 226, $ m-2" Move the content of the file from row 226th to the end of the file before the two rows in the current row. The number 0 indicates the beginning of the file, that is, before the first line of text, so 0 is equivalent to 1 -.
Search Mode:You can also specify the row address in search mode. For example
:/Pattern/d Delete the next row containing pattern;
:/Pattern/+ d Delete the next row containing pattern;
:/Pattern1/,/pattern2/d Delete the first row containing pattern1 to the first row containing pattern2;
Diagonal lines must be used as the demarcation line before and after these modes.
Redefine the location of the current row:When you use a semicolon instead of a comma, the first line address serves as the current line address, even if the cursor is not on this line. For example, ": 100; + 5 p" can display the content of 100th rows and the last 5 rows.
Global Search:
Ex has a global command g. You can search for the mode and display all the rows that contain the mode. And ": g! In contrast to ": g", this function searches for all rows that do not contain the specified mode.
: G/pattern: Find (move to) the last occurrence of pattern in the file;
: G/pattern/p: Find and display all rows containing pattern in the file;
: G! /Pattern/nu: Find and display all rows in the file that do not contain pattern, and display the row numbers of all rows found;
: 60,124g/pattern/p to search for and display the rows that contain pattern between 60th rows and 124th rows.
Merge the ex command:
To enter a new ex command, you do not need to enter a colon every time. You can use the vertical line (|) to separate the commands to merge multiple commands at the same ex prompt.
Save and exit the file:
: W writes the buffer content to the file, but does not leave. Always use ": w" when editing files ";
: Q leaves the editor;
: Wq writes the file and leaves the editor. This is an unconditional write, even if the file is not modified;
: X writes the file and leaves the editor. The file is written only when it is modified;
Summary of the vi commands in this chapter:
Ex, 1 p, 1, 2 p, d, m, co, t,: = ,:. =,:/pattern/= ,., $, %, +,-,:/pattern/d,: g,: g !, |,: W,: q,
: Wq,: x