5.2 Editing with ex
Many
ex
commands that perform normal editing operations have an
equivalent in
vi
that does the job more simply.
Obviously, you will use These ex commands are listed below, along with abbreviations for those commands. Remember that in vi each ex command must be preceded with a colon. You can use the full command name or the abbreviation, whichever is easier to remember.
5.2.1 Line Addresses
For each
ex
editing command, you have to tell
ex
which line number(s) to edit.
And for the
ex
You can specify line addresses in several ways:
Let's look at some examples. 5.2.2 Defining a Range of LinesYou can use line numbers to define explicitly a line or range of lines. Addresses that use explicit numbers are called absolute line addresses. For example:
:set number or its abbreviation:
:set nu displays line numbers. The file practice then appears:
1 With a screen editor 2 you can scroll the page, 3 move the cursor, delete lines, 4 insert characters and more The displayed line numbers are not saved when you write a file, and they do not print if you print the file. Line numbers are displayed either until you quit the vi session or until you disable the set option:
:set nonumber or:
:set nonu
To temporarily display the line numbers for a set of lines, you can use
the
:1,10# would display the line numbers from line one to line ten. As described in Chapter 3, Moving Around in a Hurry , you can also use the [CTRL-G] command to display the current line number. You can thus identify the line numbers corresponding to the start and end of a block of text by moving to the start of the block, typing [CTRL-G] then moving to the end of the block and typing [CTRL-G] again.
Yet another way to identify line numbers is with the
ex
5.2.3 Line Addressing Symbols
You can also use symbols for line addresses.
A dot (
In addition to an absolute line address, you can specify an address relative to the current line. The symbols + and - work like arithmetic operators. When placed before a number, these symbols add or subtract the value that follow. For example:
In fact, you don't need to type the dot (.) when you use + or -, because the current line is the assumed starting position. Without a number following them, + and - are equivalent to +1 and -1, respectively. [1] Similarly, ++ and -- each extend the range by an additional line, and so on. The + and - can also be used with search patterns, as shown in the next section.
The number
5.2.4 Search PatternsAnother way that ex can address lines is by using search patterns. For example:
Note that patterns are delimited by a slash both before and after . If you make deletions by pattern with vi and ex , there is a difference in the way the two editors operate. Suppose you have in your file practice the lines:
5.2.5 Redefining the Current Line PositionSometimes, using a relative line address in a command can give you unexpected results. For example, suppose the cursor is on line 1, and you want to print line 100 plus the five lines below it. If you type:
:100,+5 p you'll get an error message saying, "First address exceeds second." The reason the command fails is that the second address is calculated relative to the current cursor position (line 1), so your command is really saying this:
:100,6 p What you need is some way to tell the command to think of line 100 as the "current line," even though the cursor is on line 1. ex provides such a way. When you use a semicolon instead of a comma, the first line address is recalculated as the current line. For example, the command:
:100;+5 p prints the desired lines. The +5 is now calculated relative to line 100. A semicolon is useful with search patterns as well as absolute addresses. For example, to print the next line containing pattern , plus the 10 lines that follow it, enter the command:
:/pattern/;+10 p 5.2.6 Global Searches
You already know how to use You can use the global command on all lines in the file, or you can use line addresses to limit a global search to specified lines or to a range of lines.
As you might expect, 5.2.7 Combining ex Commands
You don't always need to type a colon to begin a new
ex
command. In
ex
, the vertical bar (
Note the use of spaces to make the commands easier to see. | ||||||||||||||||||||||||||||||
|