The next general area we will discuss is that of line addressing in ed, that is, how you specify what lines are to be affected by editing commands. We have already used constructions like 1,$s/x/y/
to specify a change on all lines.
And most users are long since familiar with
using a single newline (or return) to print the next line,
and with
to find a line that contains `thing'.
Less familiar, surprisingly enough, is the
use of
to scan
for the previous occurrence of `thing'.
This is especially handy when you realize that the thing
you want to operate on is back up the page from
where you are currently editing.

      The slash and question mark are the only characters you can use to delimit a context search, though you can use essentially any character in a substitute command.

Address Arithmetic

      The next step is to combine the line numbers like `.', `$', `/.../' and `?...?' with `+' and `-'. Thus $-1
is a command to print the next to last line of
the current file (that is, one line before line `$').
For example, to recall how far you got in a previous editing session,
prints the last six lines.
(Be sure you understand why it's six, not five.)
If there aren't six, of course, you'll get an error message.

      As another example, .-3,.+3p
prints from three lines before where you are now
(at line dot)
to three lines after,
thus giving you a bit of context.
By the way, the `+' can be omitted:
is absolutely identical in meaning.

      Another area in which you can save typing effort in specifying lines is to use `-' and `+' as line numbers by themselves. -
by itself is a command to move back up one line in the file.
In fact, you can string several minus signs together to move
back up that many lines:
moves up three lines, as does `-3'.
is also identical to the examples above.

      Since `-' is shorter than `.-1', constructions like -,.s/bad/good/
are useful. This changes `bad' to `good' on the previous line and
on the current line.

      `+' and `-' can be used in combination with searches using `/.../' and `?...?', and with `$'. The search /thing/--
finds the line containing `thing', and positions you
two lines before it.

Repeated Searches

      Suppose you ask for the search /horrible thing/
and when the line is printed you discover that it
isn't the horrible thing that you wanted,
so it is necessary to repeat the search again.
You don't have to re-type the search,
for the construction
is a shorthand for `the previous thing that was searched for',
whatever it was.
This can be repeated as many times as necessary.
You can also go backwards:
searches for the same thing,
but in the reverse direction.

      Not only can you repeat the search, but you can use `//' as the left side of a substitute command, to mean `the most recent pattern'. /horrible thing/
.... ed prints line with `horrible thing' ...
To go backwards and change a line, say
Of course, you can still use the `&' on the right hand side of a substitute to stand for
whatever got matched:
finds the next occurrence of whatever you searched for last,
replaces it by two copies of itself,
then prints the line just to verify that it worked.

Default Line Numbers and the Value of Dot

      One of the most effective ways to speed up your editing is always to know what lines will be affected by a command if you don't specify the lines it is to act on, and on what line you will be positioned (i.e., the value of dot) when a command finishes. If you can edit without specifying unnecessary line numbers, you can save a lot of typing.

      As the most obvious example, if you issue a search command like /thing/
you are left pointing at the next line that contains `thing'.
Then no address is required with commands like
to make a substitution on that line,
to print it,
to list it,
to delete it,
to append text after it,
to change it,
to insert text before it.

      What happens if there was no `thing'? Then you are left right where you were _ dot is unchanged. This is also true if you were sitting on the only `thing' when you issued the command. The same rules hold for searches that use `?...?'; the only difference is the direction in which you search.

      The delete command d leaves dot pointing at the line that followed the last deleted line. When line `$' gets deleted, however, dot points at the new line `$'.

      The line-changing commands a, c and i by default all affect the current line _ if you give no line number with them, a appends text after the current line, c changes the current line, and i inserts text before the current line.

      a, c, and i behave identically in one respect _ when you stop appending, changing or inserting, dot points at the last line entered. This is exactly what you want for typing and editing on the fly. For example, you can say a
... text ...
... botch ... (minor error)
s/botch/correct/ (fix botched line)
... more text ...
without specifying any line number for the substitute command or for
the second append command.
Or you can say
... text ...
... horrible botch ... (major error)
c (replace entire line)
... fixed up line ...

      You should experiment to determine what happens if you add no lines with a, c or i.

      The r command will read a file into the text being edited, either at the end if you give no address, or after the specified line if you do. In either case, dot points at the last line read in. Remember that you can even say 0r to read a file in at the beginning of the text. (You can also say 0a or 1i to start adding text at the beginning.)

      The w command writes out the entire file. If you precede the command by one line number, that line is written, while if you precede it by two line numbers, that range of lines is written. The w command does not change dot: the current line remains the same, regardless of what lines are written. This is true even if you say something like /^\\.AB/,/^\\.AE/w abstract
which involves a context search.

      Since the w command is so easy to use, you should save what you are editing regularly as you go along just in case the system crashes, or in case you do something foolish, like clobbering what you're editing.

      The least intuitive behavior, in a sense, is that of the s command. The rule is simple _ you are left sitting on the last line that got changed. If there were no changes, then dot is unchanged.

      To illustrate, suppose that there are three lines in the buffer, and you are sitting on the middle one: x1
Then the command
prints the third line, which is the last one changed.
But if the three lines had been
and the same command had been issued while
dot pointed
at the second line, then the result
would be to change and print only the first line,
and that is where dot would be set.

Semicolon `;'

      Searches with `/.../' and `?...?' start at the current line and move forward or backward respectively until they either find the pattern or get back to the current line. Sometimes this is not what is wanted. Suppose, for example, that the buffer contains lines like this: .
Starting at line 1, one would expect that the command
prints all the lines from the `ab' to the `bc' inclusive.
Actually this is not what happens.
(for `a' and for `b')
start from the same point, and thus they both find the line
that contains `ab'.
The result is to print a single line.
Worse, if there had been a line with a `b' in it
before the `ab' line, then the print command
would be in error, since the second line number
would be less than the first, and it is illegal to
try to print lines in reverse order.

      This is because the comma separator for line numbers doesn't set dot as each address is processed; each search starts from the same place. In ed, the semicolon `;' can be used just like comma, with the single difference that use of a semicolon forces dot to be set at that point as the line numbers are being evaluated. In effect, the semicolon `moves' dot. Thus in our example above, the command /a/;/b/p
prints the range of lines from `ab' to `bc',
because after the `a' is found, dot is set to that line,
and then `b' is searched for, starting beyond that line.

      This property is most often useful in a very simple situation. Suppose you want to find the second occurrence of `thing'. You could say /thing/
but this prints the first occurrence as well as the second,
and is a nuisance when you know very well that it is only
the second one you're interested in.
The solution is to say
This says to find the first occurrence of `thing', set dot to that line, then find the second
and print only that.

      Closely related is searching for the second previous occurrence of something, as in ?something?;??
Printing the third or fourth or ...
in either direction is left as an exercise.

      Finally, bear in mind that if you want to find the first occurrence of something in a file, starting at an arbitrary place within the file, it is not sufficient to say 1;/thing/
because this fails if `thing' occurs on line 1.
But it is possible to say
(one of the few places where 0 is a legal line number),
for this starts the search at line 1.

Interrupting the Editor

      As a final note on what dot gets set to, you should be aware that if you hit the interrupt or delete or rubout or break key while ed is doing a command, things are put back together again and your state is restored as much as possible to what it was before the command began. Naturally, some changes are irrevocable _ if you are reading or writing a file or making substitutions or deleting lines, these will be stopped in some clean but unpredictable state in the middle (which is why it is not usually wise to stop them). Dot may or may not be changed.

      Printing is more clear cut. Dot is not changed until the printing is done. Thus if you print until you see an interesting line, then hit delete, you are not sitting on that line or even near it. Dot is left where it was when the p command was started.