Mini-Unix/usr/doc/ed/e3

.H1
The current line \(mi ``Dot'' or ``.''
.H2
.PG
Suppose our buffer still contains the six lines as above,
that we have just typed
.X1
1,3p
.X2
and
.ul
ed
has printed the three lines for us.
Try typing just
.X1
p	(no line numbers).
.X2
This will print
.X1
to come to the aid of their party.
.X2
which is the third line of the buffer.
In fact it is the last
(most recent) line that we have done anything with.
(We just printed it!)
We can
repeat this ``p'' command without line numbers, and
it will continue to print line 3.
.PG
The reason is that
.ul
ed
maintains a record of the last line
that we did anything to (in this case, line 3, which we
just printed) so that it can be used instead of an explicit
line number.
This most recent line is referred to by the
shorthand symbol
.X1
.li
\fB.\fR	(pronounced ``dot'').
.X2
Dot is a line number in the same way that ``$'' is; it means
exactly ``the current line'', or loosely,
``the line we most recently did something to.''
We
can use it in several ways \(mi one possibility
is to say
.X1
.li
\fB.\fR,$p
.X2
This will print all the lines from (including) the current
line to the
end of the buffer.
In our case these are lines 3 through 6.
.PG
Some commands change the value of dot, while others do not.
The print command sets dot to the number of the last line printed;
by our last command, we would have ``\*.'' = ``$'' = 6.
.PG
Dot is most useful when used in combinations like this one:
.X1
.li
\fB.\fR+1	(or equivalently, \*.+1p)
.X2
This means ``print the next line'' and gives us a handy way to step
slowly through a buffer.
We can also say
.X1
.li
\fB.\fR\(mi1	(or \*.\(mi1p )
.X2
which means ``print the line
.ul
before
the current line.''
This enables us to go backwards if we wish.
Another useful one is something like
.X1
.li
\fB.\fR\(mi3,\*.\(mi1p
.X2
which prints the previous three lines.
.PG
Don't forget that all of these change the value of dot.
You can find out what dot is at any time by typing
.X1
.li
\fB.\fR=
.X2
.ul
Ed
will respond by printing the value of dot.
.PG
Let's summarize some things about the ``p'' command
and dot.
Essentially ``p'' can be preceded by 0, 1, or 2 line numbers.
If there is no line number given, it prints the ``current line'',
the line that dot refers to.
If there is one line number given
(with or without the letter ``p''),
it prints that line (and dot is set there); and if there
are two line numbers, it prints all the lines in that range
(and sets dot to the last line printed.)
If two line numbers are specified
the first can't be bigger than the second (see Exercise 2.)
.PG
Typing a single newline will cause printing of the next line \(mi
it's
equivalent to ``\*.+1p''.
Try it.
Try typing ``^'' \(mi it's equivalent to
``\*.\(mi1p''.
.H1
Deleting lines: the ``d'' command
.H2
.PG
Suppose we want to get rid of the three extra lines in the buffer.
This is done by the
.ul
delete
command
.X1
d
.X2
Except that ``d'' deletes lines instead of printing them,
its action is similar to that of ``p''.
The lines to be deleted are specified for ``d''
exactly as they are for ``p'':
.X1
\fIstarting line, ending line\fP d
.X2
Thus the command
.X1
4,$d
.X2
deletes lines 4 through the end.
There are now three lines left, as we can check by using
.X1
1,$p
.X2
And notice that ``$'' now is line 3!
Dot
is set to the next line after the last line deleted,
unless the last line deleted is the last line in the buffer.
In that case, dot is set to ``$''.
.H1
Exercise 4:
.H2
.PG
Experiment with ``a'', ``e'', ``r'', ``w'', ``p'', and ``d''
until you are sure that you
know what they do, and until you understand how dot, ``$'', and
line numbers are used.
.PG
If you are adventurous, try using line numbers with ``a'', ``r'', and
``w'' as well.
You will find that ``a'' will append lines
.ul
after
the line number that you specify (rather than after dot); that ``r'' reads
a file in
.ul
after
the line number you specify (not necessarily
at the end of the buffer); and that ``w'' will write out exactly the lines
you specify, not necessarily the whole buffer.
These variations are sometimes handy.
For instance you can insert a file at the beginning of a buffer
by saying
.X1
0r filename
.X2
and you can enter lines at the beginning of the buffer
by saying
.X1
0a
.li
. . . \fItext\fP . . .
.li
\fB.\fR
.X2
Notice that
``\*.w'' is
.ul
very
different from
.X1
.li
\fB.\fR
w
.X2
.H1
Modifying text: the Substitute command ``s''
.H2
.PG
We are now ready to try one of the most important
of all commands \(mi the substitute command
.X1
s
.X2
This is the command
that is used to change individual
words or letters within a line or group of lines.
It is what we use, for example, for correcting spelling
mistakes and typing errors.
.PG
Suppose that by a typing error, line 1 says
.X1
Now is th time
.X2
\(mi the ``e'' has been left off ``the''.
We can use ``s'' to fix this up as follows:
.X1
1s/th/the/
.X2
This says: ``in line 1, substitute for the characters `th'
the characters `the'.''
To verify
that it works (\fIed\fR will not print
the result automatically) we say
.X1
p
.X2
and get
.X1
Now is the time
.X2
which is what we wanted.
Notice that dot must have been set to the line
where the substitution took place, since the ``p'' command
printed that line.
Dot is always set this way with the ``s'' command.
.PG
The general way to use the substitute command is
.X1
\fIstarting-line, ending-line\fP s/\fIchange this\fP/\fIto this\fP/
.X2
Whatever string of characters is between the first pair of
slashes is replaced by whatever is between the second pair,
in
.ul
all
the lines between starting line and ending line.
Only the first occurrence on each line is changed, however.
If you want to change
.ul
every
occurrence, see Exercise 5.
The rules for line numbers are the same as those for ``p'',
except that dot is set to the last line changed.
(But there is a trap for the unwary: if no substitution
took place, dot is
.ul
not
changed.
This causes an error ``?'' as a warning.)
.PG
Thus we can say
.X1
1,$s/speling/spelling/
.X2
and correct the first spelling mistake
on each line
in the text.
(This is useful for people who are consistent
misspellers!)
.PG
If no line numbers are given, the ``s'' command assumes we mean
``make the substitution on line dot'', so it changes things only
on the current line.
This leads to the very common sequence
.X1
s/something/something else/p
.X2
which makes some correction on the
current line, and then prints it, to make sure it
worked out right.
If it didn't,
we can try again.
(Notice that we put
a print command on the same line as the substitute.
With few exceptions, ``p'' can follow any command;
no other multi-command lines are legal.)
.PG
It's also legal to say
.X1
s/ . . . //
.X2
which means ``change the first
string of characters to
\fInothing\fR'',
i.e.,
remove them.
This is useful for deleting extra words in a line or removing extra
letters from words.
For instance, if we had
.X1
Nowxx is the time
.X2
we can say
.X1
s/xx//p
.X2
to get
.X1
Now is the time
.X2
Notice that ``//'' here means ``no characters'', not a blank.
There
.ul
is
a difference!
(See below for another meaning of ``//''.)