home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Trees / V6 / usr / doc / ed / e2 < prev    next >
Encoding:
Text File  |  1975-06-26  |  6.3 KB  |  359 lines

  1. .H1
  2. Writing text out as a file \(mi the Write command ``w''
  3. .H2
  4. .PG
  5. It's likely that we'll want to save our text for later use.
  6. To write out the contents of the buffer onto a file,
  7. we use the
  8. .ul
  9. write
  10. command
  11. .X1
  12. w
  13. .X2
  14. followed by the filename we want to write on.
  15. This will copy the buffer's contents
  16. onto the specified file
  17. (destroying any previous information on the file).
  18. To save
  19. the text on a file named ``junk'', for example, type
  20. .X1
  21. w junk
  22. .X2
  23. Leave a space between ``w'' and the file name.
  24. .ul
  25. Ed
  26. will respond by printing
  27. the number of characters it wrote out.
  28. In our case,
  29. .ul
  30. ed
  31. would respond with
  32. .X1
  33. 68
  34. .X2
  35. (Remember that blanks and the newline character at the end of each
  36. line are included in the character count.)
  37. Writing a file just makes a copy of the text \(mi the
  38. buffer's contents are not disturbed, so we can go on adding
  39. lines to it.
  40. This is an important point.
  41. .ul
  42. Ed
  43. at all times works on a copy
  44. of a file, not the file itself.
  45. No change in the contents
  46. of a file takes place until you give a ``w'' command.
  47. (Writing out the text onto a file from time to time as it is being
  48. created is a good idea, since if the system crashes or if you make some horrible mistake, you will lose
  49. all the text in the buffer but any text that was written onto
  50. a file is relatively safe.)
  51. .H1
  52. Leaving ed \(mi the Quit command ``q''
  53. .H2
  54. .PG
  55. To terminate a session with
  56. .ul
  57. ed,
  58. save the text you're working on
  59. by writing it onto a file using the ``w'' command,
  60. and then type the command
  61. .X1
  62. q
  63. .X2
  64. which
  65. stands for
  66. .ul
  67. quit.
  68. The system will respond with ``%''.
  69. At
  70. this point your buffer vanishes, with all its text,
  71. which is why you want to write it out before quitting.
  72. .H1
  73. Exercise 1:
  74. .H2
  75. .PG
  76. Enter
  77. .ul
  78. ed
  79. and
  80. create some text using
  81. .X1
  82. a
  83. .li
  84. . . . text . . .
  85. .li
  86. \fB.\fR
  87. .X2
  88. Write it out using ``w''.
  89. Then leave
  90. .ul
  91. ed
  92. with the ``q'' command, and print the file,
  93. to see that everything worked.
  94. (To print a file, say
  95. .X1
  96. pr filename
  97. .X2
  98. or
  99. .X1
  100. cat filename
  101. .X2
  102. in response to ``%''.
  103. Try both.)
  104. .H1
  105. Reading text from a file \(mi the Edit command ``e''
  106. .H2
  107. .PG
  108. A common way to get text into the buffer is to read it
  109. from a file in the file system.
  110. This is what you do to edit text
  111. that you saved with the 
  112. ``w''
  113. command in a previous session.
  114. The
  115. .ul
  116. edit
  117. command ``e''
  118. fetches the entire contents of a file into the buffer.
  119. So if we had saved the three lines
  120. ``Now is the time'', etc.,
  121. with a ``w'' command in an earlier session,
  122. the 
  123. .ul
  124. ed
  125. command
  126. .X1
  127. e junk
  128. .X2
  129. would fetch the entire contents of the file ``junk''
  130. into the buffer, and respond
  131. .X1
  132. 68
  133. .X2
  134. which is the number of characters in ``junk''.
  135. .ul
  136. If anything was already in the buffer, it is deleted first.
  137. .PG
  138. If we use the ``e'' command to read a file into the buffer,
  139. then we need not use a file name after a subsequent ``w'' command;
  140. .ul
  141. ed
  142. remembers the last file name used in an ``e'' command,
  143. and ``w'' will write on this file.
  144. Thus a common way to operate is
  145. .X1
  146. ed
  147. e file
  148. [editing session]
  149. w
  150. q
  151. .X2
  152. .PG
  153. You can find out at any time what file name
  154. .ul
  155. ed
  156. is remembering by typing the 
  157. .ul
  158. file
  159. command ``f''.
  160. In our case,
  161. if we typed
  162. .X1
  163. f
  164. .X2
  165. .ul
  166. ed
  167. would reply
  168. .X1
  169. junk
  170. .X2
  171. .H1
  172. Reading text from a file \(mi the Read command ``r''
  173. .H2
  174. .PG
  175. Sometimes we want to read a file into the buffer
  176. without destroying anything that is already there.
  177. This is done by the
  178. .ul
  179. read
  180. command ``r''.
  181. The command
  182. .X1
  183. r junk
  184. .X2
  185. will read the file ``junk'' into the buffer;
  186. it adds it
  187. to the end of whatever is already in the buffer.
  188. So if we do a read after
  189. an edit:
  190. .X1
  191. e junk
  192. r junk
  193. .X2
  194. the buffer will contain
  195. .ul
  196. two
  197. copies of the text (six lines).
  198. .X1
  199. Now is the time
  200. for all good men
  201. to come to the aid of their party.
  202. Now is the time
  203. for all good men
  204. to come to the aid of their party.
  205. .X2
  206. Like the ``w'' and ``e'' commands, ``r'' prints the
  207. number of characters read in, after the reading operation is complete.
  208. .PG
  209. Generally speaking, ``r'' is much less used than ``e''.
  210. .H1
  211. Exercise 2:
  212. .H2
  213. .PG
  214. Experiment with the ``e'' command \(mi
  215. try reading and printing various files.
  216. You may get an error ``?'',
  217. typically because you spelled the file name wrong.
  218. Try alternately reading and appending to see that they work
  219. similarly.
  220. Verify that
  221. .X1
  222. ed filename
  223. .X2
  224. is exactly equivalent to
  225. .X1
  226. ed
  227. e filename
  228. .X2
  229. What does
  230. .X1
  231. f filename
  232. .X2
  233. do?
  234. .H1
  235. Printing the contents of the buffer \(mi the Print command ``p''
  236. .H2
  237. .PG
  238. To
  239. .ul
  240. print
  241. or list the contents of the buffer (or parts
  242. of it) on the terminal, we use the print command
  243. .X1
  244. p
  245. .X2
  246. The way this is done is as follows.
  247. We specify the lines where
  248. we want printing to begin and where we want it to end,
  249. separated by a comma, and
  250. followed by the letter ``p''.
  251. Thus to print the first two lines of the buffer, for
  252. example, (that is, lines 1 through 2) we say
  253. .X1
  254. 1,2p    (starting line=1, ending line=2 p)
  255. .X2
  256. .ul
  257. Ed
  258. will respond with
  259. .X1
  260. Now is the time
  261. for all good men
  262. .X2
  263. .PG
  264. Suppose we want to print
  265. .ul
  266. all
  267. the lines in the buffer.
  268. We could use ``1,3p'' as above if we knew there were exactly
  269. 3 lines in the buffer.
  270. But in general, we don't
  271. know how many there are, so what do we use for the ending
  272. line number?
  273. .ul
  274. Ed
  275. provides a shorthand symbol for ``line number of
  276. last line in buffer'' \(mi the dollar sign ``$''.
  277. Use it this
  278. way:
  279. .X1
  280. 1,$p
  281. .X2
  282. This will print
  283. .ul
  284. all
  285. the lines in the buffer (line 1 to last line.)
  286. If you want to stop the printing before it is finished,
  287. push the DEL or Delete key;
  288. .ul
  289. ed
  290. will type
  291. .X1
  292. ?
  293. .X2
  294. and wait for the next command.
  295. .PG
  296. To print the
  297. .ul
  298. last
  299. line of the buffer, we could use
  300. .X1
  301. $,$p
  302. .X2
  303. but
  304. .ul
  305. ed
  306. lets us abbreviate this to
  307. .X1
  308. $p
  309. .X2
  310. We can print any single line by typing the line
  311. number followed by a ``p''.
  312. Thus
  313. .X1
  314. 1p
  315. .X2
  316. produces the response
  317. .X1
  318. Now is the time
  319. .X2
  320. which is the first line of the buffer.
  321. .PG
  322. In fact,
  323. .ul
  324. ed
  325. lets us abbreviate even further:
  326. we can print any single line by typing
  327. .ul
  328. just
  329. the line number \(mi no need to type the letter ``p''.
  330. So if we say
  331. .X1
  332. $
  333. .X2
  334. .ul
  335. ed
  336. will print the last line of the buffer for us.
  337. .PG
  338. We can also use ``$'' in combinations like
  339. .X1
  340. $\(mi1,$p
  341. .X2
  342. which prints the last two lines of the buffer.
  343. This helps when we want to see how far we got in typing.
  344. .H1
  345. Exercise 3:
  346. .H2
  347. .PG
  348. .H2
  349. As before, create some text using the append command and
  350. experiment with the ``p'' command.
  351. You will find, for example,
  352. that you can't print line 0 or a line beyond
  353. the end of the buffer, and that attempts
  354. to print a buffer in reverse order by saying
  355. .X1
  356. 3,1p
  357. .X2
  358. don't work.
  359.