summaryrefslogtreecommitdiff
path: root/bin/ed/USD.doc/09.edtut/e3
blob: e0997fd08892cc75b6eee380e4d6510158727b64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
.\"	$OpeBSD$
.\"
.\" Copyright (C) Caldera International Inc.  2001-2002.
.\" All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code and documentation must retain the above
.\"    copyright notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\"    notice, this list of conditions and the following disclaimer in the
.\"    documentation and/or other materials provided with the distribution.
.\" 3. All advertising materials mentioning features or use of this software
.\"    must display the following acknowledgement:
.\"	This product includes software developed or owned by Caldera
.\"	International, Inc.
.\" 4. Neither the name of Caldera International, Inc. nor the names of other
.\"    contributors may be used to endorse or promote products derived from
.\"    this software without specific prior written permission.
.\"
.\" USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
.\" INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
.\" IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE FOR ANY DIRECT,
.\" INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
.\" (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
.\" SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
.\" IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
.\" POSSIBILITY OF SUCH DAMAGE.
.\"
.\"	@(#)e3	8.1 (Berkeley) 6/8/93
.\"
.SH
The current line \- ``Dot'' or ``.''
.PP
Suppose your buffer still contains the six lines as above,
that you have just typed
.P1
1,3p
.P2
and
.ul
ed
has printed the three lines for you.
Try typing just
.P1
p	(no line numbers)
.P2
This will print
.P1
to come to the aid of their party.
.P2
which is the third line of the buffer.
In fact it is the last
(most recent) line that you have done anything with.
(You just printed it!)
You can
repeat this
.UL p
command without line numbers, and
it will continue to print line 3.
.PP
The reason is that
.ul
ed
maintains a record of the last line
that you did anything to (in this case, line 3, which you
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
.P1
\&\*.	(pronounced ``dot'').
.P2
Dot is a line number in the same way that
.UL $
is; it means
exactly ``the current line'', or loosely,
``the line you most recently did something to.''
You
can use it in several ways \- one possibility
is to say
.P1
\&\*.,$p
.P2
This will print all the lines from (including) the current
line to the
end of the buffer.
In our example these are lines 3 through 6.
.PP
Some commands change the value of dot, while others do not.
The
.UL p
command sets dot to the number of the last line printed;
the last command will
set both
\*.
and
.UL $
to 6.
.PP
Dot is most useful when used in combinations like this one:
.P1
\&\*.+1	(or equivalently, \*.+1p)
.P2
This means ``print the next line'' and is a handy way to step
slowly through a buffer.
You can also say
.P1
\&\*.\-1	(or \*.\-1p )
.P2
which means ``print the line
.ul
before
the current line.''
This enables you to go backwards if you wish.
Another useful one is something like
.P1
\&\*.\-3,\*.\-1p
.P2
which prints the previous three lines.
.PP
Don't forget that all of these change the value of dot.
You can find out what dot is at any time by typing
.P1
\&\*.=
.P2
.ul
Ed
will respond by printing the value of dot.
.PP
Let's summarize some things about the
.UL p
command
and dot.
Essentially
.UL 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
.UL 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.)
.PP
Typing a single return will cause printing of the next line \-
it's
equivalent to
.UL .+1p .
Try it.
Try typing
a
.UL \- ;
you will find that
it's equivalent to
.UL .\-1p .
.SH
Deleting lines: the ``d'' command
.PP
Suppose you want to get rid of the three extra lines in the buffer.
This is done by the
.ul
delete
command
.P1
d
.P2
Except that
.UL d
deletes lines instead of printing them,
its action is similar to that of
.UL p .
The lines to be deleted are specified for
.UL d
exactly as they are for
.UL p :
.P1
\fIstarting line, ending line\fP d
.P2
Thus the command
.P1
4,$d
.P2
deletes lines 4 through the end.
There are now three lines left, as you can check by using
.P1
1,$p
.P2
And notice that
.UL $
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
.UL $ .
.SH
Exercise 4:
.PP
Experiment with
.UL a ,
.UL e ,
.UL r ,
.UL w ,
.UL p
and
.UL d
until you are sure that you
know what they do, and until you understand how dot,
.UL $ ,
and
line numbers are used.
.PP
If you are adventurous, try using line numbers with
.UL a ,
.UL r
and
.UL w
as well.
You will find that
.UL a
will append lines
.ul
after
the line number that you specify (rather than after dot); that
.UL r
reads
a file in
.ul
after
the line number you specify (not necessarily
at the end of the buffer); and that
.UL 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
.P1
0r filename
.P2
and you can enter lines at the beginning of the buffer
by saying
.P1
0a
\&. . . \fItext\fP . . .
\*.
.P2
Notice that
.UL .w
is
.ul
very
different from
.P1
\*.
w
.P2
.SH
Modifying text: the Substitute command ``s''
.PP
We are now ready to try one of the most important
of all commands \- the substitute command
.P1
s
.P2
This is the command
that is used to change individual
words or letters within a line or group of lines.
It is what you use, for example, for correcting spelling
mistakes and typing errors.
.PP
Suppose that by a typing error, line 1 says
.P1
Now is th time
.P2
\- the
.IT e
has been left off
.IT the .
You can use
.UL s
to fix this up as follows:
.P1
1s/th/the/
.P2
This says: ``in line 1, substitute for the characters
.IT th
the characters
.IT the .''
To verify
that it works
.IT ed "" (
will not print
the result automatically) say
.P1
p
.P2
and get
.P1
Now is the time
.P2
which is what you wanted.
Notice that dot must have been set to the line
where the substitution took place, since the
.UL p
command
printed that line.
Dot is always set this way with the
.UL s
command.
.PP
The general way to use the substitute command is
.P1
\fIstarting\(hyline, ending\(hyline\fP s/\fIchange this\fP/\fIto this\fP/
.P2
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
.ul
starting-line
and
.ul
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
.UL 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
.UL ?
as a warning.)
.PP
Thus you can say
.P1
1,$s/speling/spelling/
.P2
and correct the first spelling mistake
on each line
in the text.
(This is useful for people who are consistent
misspellers!)
.PP
If no line numbers are given, the
.UL 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
.P1
s/something/something else/p
.P2
which makes some correction on the
current line, and then prints it, to make sure it
worked out right.
If it didn't,
you can try again.
(Notice that there is
a
.UL p
on the same line as the
.UL s
command.
With few exceptions,
.UL p
can follow any command;
no other multi-command lines are legal.)
.PP
It's also legal to say
.P1
s/ . . . //
.P2
which means ``change the first
string of characters to
.IT nothing '', ``
i.e.,
remove them.
This is useful for deleting extra words in a line or removing extra
letters from words.
For instance, if you had
.P1
Nowxx is the time
.P2
you can say
.P1
s/xx//p
.P2
to get
.P1
Now is the time
.P2
Notice that
.UL //
(two adjacent slashes) means ``no characters'', not a blank.
There
.ul
is
a difference!
(See below for another meaning of
.UL // .)