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