Learning VI editor -- ex Editor

Source: Internet
Author: User
Tags line editor

2011-06-14 wcdj

(1) ex command
(2) Use ex for editing
(1) line address
(2) define the row range
(3) line address character
(4) Search Mode
(6) Global Search
(7) combined ex command
(3) Saving and exiting files
(1) Rename the buffer
(2) Save some files
(3) add to an existing file
(4) copy the file to another file
(5) Editing Multiple files
(1) Call VI to open multiple files
(2) Use the parameter list
(3) call a new file
(4) switch the file in vi -- Ctrl-^ is equivalent to: E #.
(5) Editing Between Files


In fact, ex is not another editor. VI is a more general and basic visual mode of the ex-line editor. Some ex commands can save a lot of editing time, so they are very useful when using vi. Most of these commands can be used without leaving VI.

(1) ex command


Although most of the edits made using VI are relatively simple, when you want to modify multiple parts of the file on a large scale, the row location of ex will show its advantages.
When you call the ex-line editor for a file, you will see information about the total number of file lines and the colon command prompt.
 
If you do not provide the ex command to display one or more lines, you will not be able to see any lines in the file.
The ex command consists of a line address (line number) and a command, which end with the Enter key. One of the most basic commands is to print (to the screen) p.
For example, if you enter 1 P after the prompt, the first line of the file will be displayed:

Note:
(1) Actually, P can be removed, because the row number itself is equivalent to the command to print the row.
(2) If you want to print multiple rows, you can specify the range of row numbers (for example, 1, 3 -- two numbers are separated by commas, and spaces in the middle are optional ).
(3) commands without line numbers are considered to act on the current line. Therefore, for example, the replacement command (s) for replacing another word with one word ). After the command is executed, the modified lines are re-displayed.
 

After using some ex commands on the file, you should call VI on the same file so that you can see a more familiar visual method of ex.
Command: VI or: Visual will convert you from ex to VI.

If you want to call the ex command in VI, you must enter a Special Base Line Character (colon), then enter the command and press the return key to execute it.
For example, in the ex Editor, if you enter the row number at the colon prompt, the row will be moved to the row. If you want to use this command in VI to move the cursor to the 6th line of the file, you can enter: 6 and then press the return key.

(2) Use ex for editing


Many ex commands that complete normal editing operations have equivalent commands that are easier to accomplish the same job in VI. Obviously, you will use DW or dd instead of using the DELETE command in ex to delete a single word or line. However, because the ex command allows you to use a single command to modify large text blocks, the ex command is more useful when you want to modify many lines.

Note: Each ex command in VI must have a colon before it. You can use the complete command or its abbreviation. The key is which one is easier to remember.
Command functions
Delete D delete a row
Move M move row
Copy CO or t copy row

(1) line address

For each ex edit command, the line to be edited by Ex must be provided. For the ex move and copy commands, you also need to provide the target location for ex to move or copy the text.

You can use the following methods to specify the row address:
[1] use a clear row number.
[2] use a symbol that helps you specify the row number relative to the current position in the file.
[3] use the search mode as the address of the row to be edited.

(2) define the row range

You can use a line number to clearly define the range of a row or line. An address with a definite number is called an absolute line address.
For example:
: 3, 18D delete 3 ~ 18 rows
: 160,224 M23 mobile 160 ~ The rows from 224 to 23
: 23, 29co100 replication 23 ~ 29 rows and paste them to the end of 100 rows

Note:
(1) to make it easier to edit row numbers, you can display all row numbers on the left of the screen. Command: set number or: Set nu or: se Nu. Cancel: Set nonumber.
(2) You can use the # symbol to temporarily display the row numbers of some rows. For example, 1, 10 # displays the row number from 1 to 10.
(3) You can also use CTRL-G commands to display the current row number.
(4) another way to specify a row number is to use the = command of ex.
: = Displays the total number of rows.
:. = Display the row number of the current row
:/Pattern/= display the row number of the first row that matches the pattern

(3) line address character

You can also use a line address symbol.
Point (.) current row
$ The last line of the file
% Each row in the file, which is equivalent to a combination of 1 and $

For example:
:., $ D delete text from the current row to the end of the file
: 20,. M $ move the text from 20 rows to the end of the file
: % D delete all rows in the file
: % T $ copy all rows and paste them to the end of the file (as a continuous copy)
 
In addition to the absolute row address, you can also specify an address relative to the current row. Symbols + and-are like mathematical operators. When placed before a number, these symbols add or subtract the values following them. For example:
:... + 20d Delete 20 rows starting from the current row
: 226, $ M.-2 move the text from row 226 to the end of the file to the second row above the current row
:., + 20 # display the row number of the 20 rows below the current row (in fact, because the current start position is assumed, you do not need to enter a dot (.) When using + or (.))

Note:
(1) If there are no numbers after + and-, they are equivalent to + 1 and-1 respectively.
(2) ++ and -- are equivalent to + 2 and-2.
(3) In the relative address, the plus sign or minus sign cannot be separated from the number next to it. For example, "+ 10" indicates "the next 10 rows", and "+ 10" indicates "the next 11 (1 + 10) rows". This may not be our expected or expected result.
(4) The number 0 indicates the beginning of the file (fictitious 0 rows ). 0 is equivalent to 1 -.
:-, + T0 copy the three rows (from the rows above the cursor to the rows below the cursor) and paste them to the beginning of the file.

(4) Search Mode

Another way for ex to locate rows is to use the search mode. For example:
:/Pattern/d Delete the next row containing Pattern
:/Pattern/+ D Delete the following rows of the next row containing pattern (you can also use + 1 to replace individual +)
:/Pattern1/,/pattern2/D will delete content from the first line containing pattern1 to the first line containing pattern2
:.,/Pattern/M23 move the text from the current line (.) to the first line that contains pattern to the end of 23 rows

Note:
(1) the front and back of the pattern must be bounded by a slash.
(2) If you use the delete mode in VI and ex, you will find that the two editors are operated differently. For example:
D/while vi delete command to delete the text from the cursor position to the word while, and keep the remaining parts of the two lines.
:.,/While/d ex command will delete all the specified rows. In this case, it refers to the current row and the row in the include mode. All rows within their range will be deleted.


Sometimes, using a relative line address in a command may lead to unexpected results.
For example, if the cursor is in the first line, we want to display row 100th and the five rows below it. If you enter:
: 100, + 5 p
We will get the error message "first address exceeds second. The command fails because the second line address is calculated relative to the current cursor position (the first line), so the command is actually:
: 100, 6 p
What we need is to make this command treat line 100th as the "current line" method, even if the cursor is at the first line.

Ex provides the following method: when you use a semicolon instead of a comma, the first row address is treated as the current row and recalculated. For example:
: 100; + 5 p is equivalent to: 100,106 P
:/Pattern/; + 10 P: display the next line containing pattern and the 10 lines below it

(6) Global Search

Use a slash (/) in VI to search for character patterns in a file.
Ex also has the global command g, which allows you to search for the mode and display all the lines that contain the mode. Command: G! Is opposite to: G, use: G! (Or its synonym: V) You can search for rows that do not contain the mode.
You can use global commands for all lines in the file, or use the line address to restrict global search to a specified row or line range.
: G/pattern: Find the position (??? I tried all the output, which is the same as g/pattern/P)
: G/pattern/P: Find and display all rows in the file containing Pattern
: G! /Pattern/nu searches for and displays all rows in the file that do not contain pattern, and displays the row numbers of these rows.
: 60,124g/pattern/P to search for and display all rows with pattern between 60th rows and 124 rows

Note: You can also use g for global replacement.

(7) combined ex command

You do not have to enter a colon to start the new ex command. In ex, the vertical bar (|) is the command separator, it allows you to combine multiple commands in the same ex prompt (similar to this, multiple commands are separated by semicolons ).
When using |, pay attention to the specified line address. If a command affects the order of the row in the file, the next command will work with the new row location. For example:
: 1, 3D | S/thier/their/Delete lines 1 to 3, and then replace them in the current row (this row is the 4th rows before the ex prompt is called ).
: 1, 5 M 10 | G/pattern/nu move rows 1 to 5 to the end of row 10th, and then display all rows and row numbers containing Pattern

Note: using spaces makes the command easier to read.

(3) Saving and exiting files


We have learned how to exit and write (SAVE) the VI command ZZ of the file. However, because the ex command can be better controlled, users often want to use them to exit the file.
: W writes (SAVE) the buffer to the file but does not exit. It can (and should) be used during session Editing: W to protect the editing operation to avoid system paralysis or major editing errors.
: Q: exit the Editor (and return to the Unix prompt)
: WQ writes a file and exits the editor. Write operations are performed unconditionally even if the file is not modified.
: X write the file and exit the editor. Only after the file is modified can the write operation be performed (Note:
: When you edit the source code and use make to execute operations based on the file modification time, the difference between WQ and .)

VI protects existing files in the buffer zone and your edits. For example, if you edit the file by calling VI, but exit without saving and editing, VI will give the following error message:
No write since last change.
Although these warnings can prevent serious errors, you may need to execute these commands and add an exclamation point (!) after the command (!) The VI warning is ignored. Example: W! Or: Q!
Note:
(1): W! You can save the edits to a file opened in read-only mode using vi-R or view (assuming that you have the write permission on the file)
(2): Q! A command is a basic editing command that allows you to exit without affecting the original text and ignore all edits made in this session. The content in the buffer will be deleted.

(1) Rename the buffer

You can also use W to save the entire buffer (a copy of the file being edited) to the new file.

Assume that the "practice" file contains 600 rows is opened and a large number of edits are made. If you want to exit but want to save the old version of "practice" and the new version for comparison, use the following command to save the edited buffer to the name "practice. in the new file:
: W practice. New

(2) Save some files

Sometimes, you only want to save a part of the file as a new file.
To save part of the file text, you can combine the ex line address with the write command W. For example:
: 230, $ W newfile: Save the text from row 230th to the end of the file to "newfile ".
:., 600 W newfile will save the text from the current row to 600th rows to "newfile"

(3) add to an existing file

You can add all or part of the buffer to the end of an existing file using the Unix redirection modifier (>) and W. For example:
: 1, 10 W newfile | 340, $ w "newfile" file will contain content from 1 to 10 rows and from 340 rows to the end of the buffer

(4) copy the file to another file


Sometimes you want to copy the existing text or data in the system to the file being edited. In Vi, you can use the ex command to read the content of another file:
: Read filename
Or use the abbreviation:
: R filename
This command inserts the content of the filename file into the beginning of the line after the cursor position in this file. To specify a row that is not where the cursor is located, you only need to enter the row number (or other line address) before the read and R commands.
: 185 R/home/wcdj/data read the same file and put it behind 185th rows

Note: Read the entire file, not part of the file.

Other methods to read files include:
: $ R/home/wcdj/data put the file to be read at the end of the current file
: 0 r/home/wcdj/data put the file to be read at the beginning of the current file
:/Pattern/R/home/wcdj/data put the file to be read in the current file after the row containing Pattern

(5) Editing Multiple files


The ex command can be switched between multiple files. The advantage of Editing Multiple files at the same time is that the speed is fast. If you are sharing the system with other users, it takes time to exit the current file and then enter VI with the file to be edited. In the same editing session, switching between files not only accelerates access, but also retains the specified abbreviations and command sequences, as well as the replication buffer, so that text can be copied between files.

(1) Call VI to open multiple files

When calling VI for the first time, you can specify multiple files to be edited, and then use the ex command to switch between files. For example:
$ VI file1 file2
Edit file1 first. After editing the first file, run the: W command of ex to write (SAVE) file1 and: N command to call the next file file2.

(2) Use the parameter list

In the parameter list, the ex command is not just to use: n to move to the next file. : The args command (AR) lists the Files specified on the command line. The current file is enclosed in square brackets.
: Rewind (: rew) command to reset the current file to the first file specified on the command line. "First <="
: The last command (provided by Elvis and VIM) is used to move to the last file on the command line. "=> Last"

(3) call a new file

You do not have to call multiple files at the beginning of the edit session. You can use the EX: e command to switch to another file at any time.
To edit another file in VI, first save the current file (: W) and run the following command:
: E filename

At a time point, VI records these two file names as the current and backup file names. You can also specify them by using the symbols % (current file name) and # (Alternate file name.
Because # allows you to switch back and forth between two files, that is,: E # (Note: only valid for the two adjacent files)
: E! Discard the edit operation and return to the last saved version of the current file.
Compared with #, % is mainly used to write the content of the current buffer to the new file. Because % represents the current file name (assuming the current file name is file ),
: W file. New is equivalent to w %. New.

(4) switch the file in vi -- Ctrl-^ is equivalent to: E #.

Because switching to the previous file often happens, you do not have to move to the ex command to perform this operation.
VI ^ command (Control Key + insert symbol key) can achieve this function. Use this command is the same as input: E.

Note: similar to: E #, if the current buffer is not saved, VI cannot switch to the previous file. (??? If you try it, you can switch it if it is not saved)

(5) Editing Between Files

When a single letter name is specified for the replication buffer, you can easily move text from one file to another.
When you use the: e command to load a new file to the VI buffer, the named buffer will not be cleared. Therefore, by copying or deleting text from a file (if needed, you can go to multiple named buffers), use: E to call the new file, then paste the name buffer into the new file so that text can be transferred between files.
For example:
$ VI file1 open file1
"F4yy copies four rows to the buffer zone F
: W save the file
: E file2 use: E to enter file file2, move the cursor to the location of the text to be copied
"FP pasted the copied text in buffer f Under the cursor

Note: The other method to move text from one file to another is to use the EX: YA (copy) and: Pu (paste) commands. Although these commands work in the same way as the equivalent VI commands: Y and P, they must be used together with the row address range of ex and the named buffer.
For example:
: 160,224 ya a will copy lines 160th to 224 to buffer A, and then you can use the: e command to move to the file where you want to place these lines
: PU a paste the content in buffer A to the end of the current row


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.