BSD 4_4 release
[unix-history] / usr / src / bin / ed / USD.doc / advanced / ae6
CommitLineData
ad787160
C
1.\" This module is believed to contain source code proprietary to AT&T.
2.\" Use and redistribution is subject to the Berkeley Software License
3.\" Agreement and your Software Agreement with AT&T (Western Electric).
ff262511 4.\"
ad787160 5.\" @(#)ae6 8.1 (Berkeley) 6/8/93
df9d6ce9
KM
6.\"
7.NH
8CUT AND PASTE WITH THE EDITOR
9.PP
10Now we move on to manipulating pieces of files _
11individual lines or groups of lines.
12This is another area where new users seem
13unsure of themselves.
14.SH
15Filenames
16.PP
17The first step is to ensure that you know the
18.UL ed
19commands for reading and writing files.
20Of course you can't go very far without knowing
21.UL r
22and
23.UL w .
24Equally useful, but less well known, is the `edit' command
25.UL e .
26Within
27.UL ed ,
28the command
29.P1
30e newfile
31.P2
32says `I want to edit a new file called
33.ul
34newfile,
35without leaving the editor.'
36The
37.UL e
38command discards whatever you're currently working on
39and starts over on
40.ul
41newfile.
42It's exactly the same as if you had quit with the
43.UL q
44command, then re-entered
45.UL ed
46with a new file name,
47except that if you have a pattern remembered, then a command
48like
49.UL //
50will still work.
51.PP
52If you enter
53.UL ed
54with the command
55.P1
56ed file
57.P2
58.UL ed
59remembers the name of the file,
60and any subsequent
61.UL e ,
62.UL r
63or
64.UL w
65commands that don't contain a filename
66will refer to this remembered file.
67Thus
68.P1 2
69.ta .5i .6i .7i
70ed file1
71 ... (editing) ...
72w (writes back in file1)
73e file2 (edit new file, without leaving editor)
74 ... (editing on file2) ...
75w (writes back on file2)
76.P2
77(and so on) does a series of edits on various files
78without ever leaving
79.UL ed
80and without typing the name of any file more than once.
81(As an aside, if you examine the sequence of commands here,
82you can see why many
83UNIX
84systems use
85.UL e
86as a synonym
87for
88.UL ed .)
89.PP
90You can find out the remembered file name at any time
91with the
92.UL f
93command;
94just type
95.UL f
96without a file name.
97You can also change the name of the remembered file name with
98.UL f ;
99a useful sequence is
100.P1
101ed precious
102f junk
103 ... (editing) ...
104.P2
105which gets a copy of a precious file,
106then uses
107.UL f
108to guarantee that a careless
109.UL w
110command won't clobber the original.
111.SH
112Inserting One File into Another
113.PP
114Suppose you have a file called
115`memo',
116and you want the file called
117`table'
118to be inserted just after the reference to
119Table 1.
120That is, in
121`memo'
122somewhere is a line that says
123.IP
124Table 1 shows that ...
125.LP
126and the data contained in
127`table'
128has to go there,
129probably so it will be formatted
130properly by
131.UL nroff
132or
133.UL troff .
134Now what?
135.PP
136This one is easy.
137Edit
138`memo',
139find
140`Table 1',
141and add the file
142`table'
143right there:
144.P1
145ed memo
146/Table 1/
147.ft I
148Table 1 shows that ... [response from ed]
149.ft
150\&\*.r table
151.P2
152The critical line is the last one.
153As we said earlier, the
154.UL r
155command reads a file;
156here you asked for it to be read in right after
157line dot.
158An
159.UL r
160command without any address
161adds lines at the end,
162so it is the same as
163.UL $r .
164.SH
165Writing out Part of a File
166.PP
167The other side of the coin is writing out part of
168the document you're editing.
169For example, maybe
755d1138 170you want to copy out into a separate file
df9d6ce9
KM
171that table from the previous example,
172so it can be formatted and tested separately.
173Suppose that in the file being edited
174we have
175.P1
176\&\*.TS
177 ...[lots of stuff]
178\&\*.TE
179.P2
180which is the way a table is set up for the
181.UL tbl
182program.
183To isolate
184the table
185in a separate file called
186`table',
187first find the start of the table
188(the `.TS' line), then write out the interesting part:
189.P1
190/^\*e\*.TS/
191.ft I
192\&\*.TS [ed prints the line it found]
193.ft R
194\&\*.,/^\*e\*.TE/w table
195.P2
196and the job is done.
197If you are confident, you can do it all at once with
198.P1
199/^\*e\*.TS/;/^\*e\*.TE/w table
200.P2
755d1138
KM
201and now you have two copies, one in the file you're still editing,
202one in the file `table' you've just written.
df9d6ce9
KM
203.PP
204The point is that the
205.UL w
206command can
207write out a group of lines, instead of the whole file.
208In fact, you can write out a single line if you like;
209just give one line number instead of two.
210For example, if you have just typed a horribly complicated line
211and you know that it (or something like it) is going to be needed later,
212then save it _ don't re-type it.
213In the editor, say
214.P1
215a
216\&...lots of stuff...
217\&...horrible line...
218\&\*.
219\&\*.w temp
220a
221\&\*.\*.\*.more stuff\*.\*.\*.
222\&\*.
223\&\*.r temp
224a
225\&\*.\*.\*.more stuff\*.\*.\*.
226\&\*.
227.P2
228This last example is worth studying, to be sure you appreciate
229what's going on.
230.SH
231Moving Lines Around
232.PP
233Suppose you want to
234move a paragraph from its present position in a paper
235to the end.
236How would you do it?
237As a concrete example, suppose each paragraph in the paper
238begins with the formatting command
239`.PP'.
240Think about it and write down the details before reading on.
241.PP
242The brute force way
243(not necessarily bad)
244is to write the paragraph onto a temporary file,
245delete it from its current position,
246then read in the temporary file at the end.
247Assuming that you are sitting on the
248`.PP' command that begins
249the paragraph, this is the sequence of commands:
250.P1
251\&\*.,/^\*e\*.PP/-w temp
252\&\*.,//-d
253$r temp
254.P2
255That is, from where you are now
256(`\*.')
257until one line before the next `\*.PP'
258(`/^\*e\*.PP/\-')
259write onto
260`temp'.
261Then delete the same lines.
262Finally, read
263`temp'
264at the end.
265.PP
266As we said, that's the brute force way.
267The easier way (often)
268is to use the
269.ul
270move
271command
272.UL m
273that
274.UL ed
275provides _
276it lets you do the whole set of operations
277at one crack,
278without any temporary file.
279.PP
280The
281.UL m
282command
283is like many other
284.UL ed
285commands in that it takes up to two line numbers in front
286that tell what lines are to be affected.
287It is also
288.ul
289followed
290by a line number that tells where the lines are to go.
291Thus
292.P1
293line1, line2 m line3
294.P2
295says to move all the lines between
296`line1'
297and
298`line2'
299after
300`line3'.
301Naturally, any of
302`line1'
303etc., can be patterns between slashes,
304$
305signs, or other ways to specify lines.
306.PP
307Suppose again that you're sitting at the first line of the
308paragraph.
309Then you can say
310.P1
311\&\*.,/^\*e\*.PP/-m$
312.P2
313That's all.
314.PP
315As another example of a frequent operation,
316you can reverse the order of two adjacent lines
317by moving the first one
318to after the second.
319Suppose that you are positioned at the first.
320Then
321.P1
322m+
323.P2
324does it.
325It says to move line dot to after one line after line dot.
326If you are positioned on the second line,
327.P1
328m--
329.P2
330does the interchange.
331.PP
332As you can see, the
333.UL m
334command is more succinct and direct than
335writing, deleting and re-reading.
336When is brute force better anyway?
337This is a matter of personal taste _
338do what you have most confidence in.
339The main difficulty with the
340.UL m
341command
342is that if you use patterns to specify both the lines
343you are moving and the target,
344you have to take care that you specify them properly,
345or you may well not move the lines you thought you did.
346The result of a botched
347.UL m
348command can be a ghastly mess.
349Doing the job a step at a time
350makes it easier for you to verify at each step
351that you accomplished what you wanted to.
352It's also a good idea to issue a
353.UL w
354command
355before doing anything complicated;
356then if you goof, it's easy to back up
357to where you were.
358.SH
359Marks
360.PP
361.UL ed
362provides a facility for marking a line
363with a particular name so you can later reference it
364by name
365regardless of its actual line number.
366This can be handy for moving lines,
755d1138 367and for keeping track of them even after they've been moved.
df9d6ce9
KM
368The
369.ul
370mark
371command is
372.UL k ;
373the command
374.P1
375kx
376.P2
377marks the current line with the name `x'.
378If a line number precedes the
379.UL k ,
380that line is marked.
381(The mark name must be a single lower case letter.)
382Now you can refer to the marked line with the address
383.P1
384\(fmx
385.P2
386.PP
387Marks are most useful for moving things around.
388Find the first line of the block to be moved, and mark it
389with
390.ul
391\(fma.
392Then find the last line and mark it with
393.ul
394\(fmb.
395Now position yourself at the place where the stuff is to go
396and say
397.P1
398\(fma,\(fmbm\*.
399.P2
400.PP
401Bear in mind that only one line can have a particular
402mark name associated with it
403at any given time.
404.SH
405Copying Lines
406.PP
407We mentioned earlier the idea of saving a line
408that was hard to type or used often,
409so as to cut down on typing time.
410Of course this could be more than one line;
411then the saving is presumably even greater.
412.PP
413.UL ed
414provides another command,
415called
416.UL t
417(for `transfer')
418for making a copy of a group of one or more lines
419at any point.
420This is often easier than writing and reading.
421.PP
422The
423.UL t
424command is identical to the
425.UL m
426command, except that instead of moving lines
427it simply duplicates them at the place you named.
428Thus
429.P1
4301,$t$
431.P2
432duplicates the entire contents that you are editing.
433A more common use for
434.UL t
435is for creating a series of lines that differ only slightly.
436For example, you can say
437.P1
438.ta 1i
439a
440\&.......... x ......... (long line)
441\&\*.
442t\*. (make a copy)
443s/x/y/ (change it a bit)
444t\*. (make third copy)
445s/y/z/ (change it a bit)
446.P2
447and so on.
448.SH
449The Temporary Escape `!'
450.PP
451Sometimes it is convenient to be able
452to temporarily escape from the editor to do
453some other
454.UX
455command,
456perhaps one of the file copy or move commands
457discussed in section 5,
458without leaving the editor.
459The `escape' command
460.UL !
461provides a way to do this.
462.PP
463If you say
464.P1
465!any UNIX command
466.P2
467your current editing state is suspended,
468and the
469.UX
470command you asked for is executed.
471When the command finishes,
472.UL ed
473will signal you by printing another
474.UL ! ;
475at that point you can resume editing.
476.PP
477You can really do
478.ul
479any
480.UX
481command, including another
482.UL ed .
483(This is quite common, in fact.)
484In this case, you can even do another
485.UL ! .
755d1138
KM
486.PP
487On Berkeley
488.UX
489systems, there is an additional (and preferable) mechanism called
490.ul
491job control
492which lets you suspend your edit session (or, for that matter,
493any program), return to the shell from
494which you invoked that program, and issue any commands, then resume
495the program from the point where it was stopped. See
496.ul
497An Introduction to the C Shell
498for more details.