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