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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
|
.\" $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.
.\"
.\" @(#)ae6 8.1 (Berkeley) 6/8/93
.\"
.NH
CUT AND PASTE WITH THE EDITOR
.PP
Now we move on to manipulating pieces of files _
individual lines or groups of lines.
This is another area where new users seem
unsure of themselves.
.SH
Filenames
.PP
The first step is to ensure that you know the
.UL ed
commands for reading and writing files.
Of course you can't go very far without knowing
.UL r
and
.UL w .
Equally useful, but less well known, is the `edit' command
.UL e .
Within
.UL ed ,
the command
.P1
e newfile
.P2
says `I want to edit a new file called
.ul
newfile,
without leaving the editor.'
The
.UL e
command discards whatever you're currently working on
and starts over on
.ul
newfile.
It's exactly the same as if you had quit with the
.UL q
command, then re-entered
.UL ed
with a new file name,
except that if you have a pattern remembered, then a command
like
.UL //
will still work.
.PP
If you enter
.UL ed
with the command
.P1
ed file
.P2
.UL ed
remembers the name of the file,
and any subsequent
.UL e ,
.UL r
or
.UL w
commands that don't contain a filename
will refer to this remembered file.
Thus
.P1 2
.ta .5i .6i .7i
ed file1
... (editing) ...
w (writes back in file1)
e file2 (edit new file, without leaving editor)
... (editing on file2) ...
w (writes back on file2)
.P2
(and so on) does a series of edits on various files
without ever leaving
.UL ed
and without typing the name of any file more than once.
(As an aside, if you examine the sequence of commands here,
you can see why many
UNIX
systems use
.UL e
as a synonym
for
.UL ed .)
.PP
You can find out the remembered file name at any time
with the
.UL f
command;
just type
.UL f
without a file name.
You can also change the name of the remembered file name with
.UL f ;
a useful sequence is
.P1
ed precious
f junk
... (editing) ...
.P2
which gets a copy of a precious file,
then uses
.UL f
to guarantee that a careless
.UL w
command won't clobber the original.
.SH
Inserting One File into Another
.PP
Suppose you have a file called
`memo',
and you want the file called
`table'
to be inserted just after the reference to
Table 1.
That is, in
`memo'
somewhere is a line that says
.IP
Table 1 shows that ...
.LP
and the data contained in
`table'
has to go there,
probably so it will be formatted
properly by
.UL nroff
or
.UL troff .
Now what?
.PP
This one is easy.
Edit
`memo',
find
`Table 1',
and add the file
`table'
right there:
.P1
ed memo
/Table 1/
.ft I
Table 1 shows that ... [response from ed]
.ft
\&\*.r table
.P2
The critical line is the last one.
As we said earlier, the
.UL r
command reads a file;
here you asked for it to be read in right after
line dot.
An
.UL r
command without any address
adds lines at the end,
so it is the same as
.UL $r .
.SH
Writing out Part of a File
.PP
The other side of the coin is writing out part of
the document you're editing.
For example, maybe
you want to copy out into a separate file
that table from the previous example,
so it can be formatted and tested separately.
Suppose that in the file being edited
we have
.P1
\&\*.TS
...[lots of stuff]
\&\*.TE
.P2
which is the way a table is set up for the
.UL tbl
program.
To isolate
the table
in a separate file called
`table',
first find the start of the table
(the `.TS' line), then write out the interesting part:
.P1
/^\*e\*.TS/
.ft I
\&\*.TS [ed prints the line it found]
.ft R
\&\*.,/^\*e\*.TE/w table
.P2
and the job is done.
If you are confident, you can do it all at once with
.P1
/^\*e\*.TS/;/^\*e\*.TE/w table
.P2
and now you have two copies, one in the file you're still editing,
one in the file `table' you've just written.
.PP
The point is that the
.UL w
command can
write out a group of lines, instead of the whole file.
In fact, you can write out a single line if you like;
just give one line number instead of two.
For example, if you have just typed a horribly complicated line
and you know that it (or something like it) is going to be needed later,
then save it _ don't re-type it.
In the editor, say
.P1
a
\&...lots of stuff...
\&...horrible line...
\&\*.
\&\*.w temp
a
\&\*.\*.\*.more stuff\*.\*.\*.
\&\*.
\&\*.r temp
a
\&\*.\*.\*.more stuff\*.\*.\*.
\&\*.
.P2
This last example is worth studying, to be sure you appreciate
what's going on.
.SH
Moving Lines Around
.PP
Suppose you want to
move a paragraph from its present position in a paper
to the end.
How would you do it?
As a concrete example, suppose each paragraph in the paper
begins with the formatting command
`.PP'.
Think about it and write down the details before reading on.
.PP
The brute force way
(not necessarily bad)
is to write the paragraph onto a temporary file,
delete it from its current position,
then read in the temporary file at the end.
Assuming that you are sitting on the
`.PP' command that begins
the paragraph, this is the sequence of commands:
.P1
\&\*.,/^\*e\*.PP/-w temp
\&\*.,//-d
$r temp
.P2
That is, from where you are now
(`\*.')
until one line before the next `\*.PP'
(`/^\*e\*.PP/\-')
write onto
`temp'.
Then delete the same lines.
Finally, read
`temp'
at the end.
.PP
As we said, that's the brute force way.
The easier way (often)
is to use the
.ul
move
command
.UL m
that
.UL ed
provides _
it lets you do the whole set of operations
at one crack,
without any temporary file.
.PP
The
.UL m
command
is like many other
.UL ed
commands in that it takes up to two line numbers in front
that tell what lines are to be affected.
It is also
.ul
followed
by a line number that tells where the lines are to go.
Thus
.P1
line1, line2 m line3
.P2
says to move all the lines between
`line1'
and
`line2'
after
`line3'.
Naturally, any of
`line1'
etc., can be patterns between slashes,
$
signs, or other ways to specify lines.
.PP
Suppose again that you're sitting at the first line of the
paragraph.
Then you can say
.P1
\&\*.,/^\*e\*.PP/-m$
.P2
That's all.
.PP
As another example of a frequent operation,
you can reverse the order of two adjacent lines
by moving the first one
to after the second.
Suppose that you are positioned at the first.
Then
.P1
m+
.P2
does it.
It says to move line dot to after one line after line dot.
If you are positioned on the second line,
.P1
m--
.P2
does the interchange.
.PP
As you can see, the
.UL m
command is more succinct and direct than
writing, deleting and re-reading.
When is brute force better anyway?
This is a matter of personal taste _
do what you have most confidence in.
The main difficulty with the
.UL m
command
is that if you use patterns to specify both the lines
you are moving and the target,
you have to take care that you specify them properly,
or you may well not move the lines you thought you did.
The result of a botched
.UL m
command can be a ghastly mess.
Doing the job a step at a time
makes it easier for you to verify at each step
that you accomplished what you wanted to.
It's also a good idea to issue a
.UL w
command
before doing anything complicated;
then if you goof, it's easy to back up
to where you were.
.SH
Marks
.PP
.UL ed
provides a facility for marking a line
with a particular name so you can later reference it
by name
regardless of its actual line number.
This can be handy for moving lines,
and for keeping track of them even after they've been moved.
The
.ul
mark
command is
.UL k ;
the command
.P1
kx
.P2
marks the current line with the name `x'.
If a line number precedes the
.UL k ,
that line is marked.
(The mark name must be a single lower case letter.)
Now you can refer to the marked line with the address
.P1
\(fmx
.P2
.PP
Marks are most useful for moving things around.
Find the first line of the block to be moved, and mark it
with
.ul
\(fma.
Then find the last line and mark it with
.ul
\(fmb.
Now position yourself at the place where the stuff is to go
and say
.P1
\(fma,\(fmbm\*.
.P2
.PP
Bear in mind that only one line can have a particular
mark name associated with it
at any given time.
.SH
Copying Lines
.PP
We mentioned earlier the idea of saving a line
that was hard to type or used often,
so as to cut down on typing time.
Of course this could be more than one line;
then the saving is presumably even greater.
.PP
.UL ed
provides another command,
called
.UL t
(for `transfer')
for making a copy of a group of one or more lines
at any point.
This is often easier than writing and reading.
.PP
The
.UL t
command is identical to the
.UL m
command, except that instead of moving lines
it simply duplicates them at the place you named.
Thus
.P1
1,$t$
.P2
duplicates the entire contents that you are editing.
A more common use for
.UL t
is for creating a series of lines that differ only slightly.
For example, you can say
.P1
.ta 1i
a
\&.......... x ......... (long line)
\&\*.
t\*. (make a copy)
s/x/y/ (change it a bit)
t\*. (make third copy)
s/y/z/ (change it a bit)
.P2
and so on.
.SH
The Temporary Escape `!'
.PP
Sometimes it is convenient to be able
to temporarily escape from the editor to do
some other
.UX
command,
perhaps one of the file copy or move commands
discussed in section 5,
without leaving the editor.
The `escape' command
.UL !
provides a way to do this.
.PP
If you say
.P1
!any UNIX command
.P2
your current editing state is suspended,
and the
.UX
command you asked for is executed.
When the command finishes,
.UL ed
will signal you by printing another
.UL ! ;
at that point you can resume editing.
.PP
You can really do
.ul
any
.UX
command, including another
.UL ed .
(This is quite common, in fact.)
In this case, you can even do another
.UL ! .
.PP
On Berkeley
.UX
systems, there is an additional (and preferable) mechanism called
.ul
job control
which lets you suspend your edit session (or, for that matter,
any program), return to the shell from
which you invoked that program, and issue any commands, then resume
the program from the point where it was stopped. See
.ul
An Introduction to the C Shell
for more details.
|