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