home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Trees / V7 / usr / doc / adv.ed / ae6 < prev    next >
Encoding:
Text File  |  1979-01-10  |  9.1 KB  |  478 lines

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