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

  1. .H1
  2. The current line \(mi ``Dot'' or ``.''
  3. .H2
  4. .PG
  5. Suppose our buffer still contains the six lines as above,
  6. that we have just typed
  7. .X1
  8. 1,3p
  9. .X2
  10. and
  11. .ul
  12. ed
  13. has printed the three lines for us.
  14. Try typing just
  15. .X1
  16. p    (no line numbers).
  17. .X2
  18. This will print
  19. .X1
  20. to come to the aid of their party.
  21. .X2
  22. which is the third line of the buffer.
  23. In fact it is the last
  24. (most recent) line that we have done anything with.
  25. (We just printed it!)
  26. We can
  27. repeat this ``p'' command without line numbers, and
  28. it will continue to print line 3.
  29. .PG
  30. The reason is that
  31. .ul
  32. ed
  33. maintains a record of the last line
  34. that we did anything to (in this case, line 3, which we
  35. just printed) so that it can be used instead of an explicit
  36. line number.
  37. This most recent line is referred to by the
  38. shorthand symbol
  39. .X1
  40. .li
  41. \fB.\fR    (pronounced ``dot'').
  42. .X2
  43. Dot is a line number in the same way that ``$'' is; it means
  44. exactly ``the current line'', or loosely,
  45. ``the line we most recently did something to.''
  46. We
  47. can use it in several ways \(mi one possibility
  48. is to say
  49. .X1
  50. .li
  51. \fB.\fR,$p
  52. .X2
  53. This will print all the lines from (including) the current
  54. line to the
  55. end of the buffer.
  56. In our case these are lines 3 through 6.
  57. .PG
  58. Some commands change the value of dot, while others do not.
  59. The print command sets dot to the number of the last line printed;
  60. by our last command, we would have ``\*.'' = ``$'' = 6.
  61. .PG
  62. Dot is most useful when used in combinations like this one:
  63. .X1
  64. .li
  65. \fB.\fR+1    (or equivalently, \*.+1p)
  66. .X2
  67. This means ``print the next line'' and gives us a handy way to step
  68. slowly through a buffer.
  69. We can also say
  70. .X1
  71. .li
  72. \fB.\fR\(mi1    (or \*.\(mi1p )
  73. .X2
  74. which means ``print the line
  75. .ul
  76. before
  77. the current line.''
  78. This enables us to go backwards if we wish.
  79. Another useful one is something like
  80. .X1
  81. .li
  82. \fB.\fR\(mi3,\*.\(mi1p
  83. .X2
  84. which prints the previous three lines.
  85. .PG
  86. Don't forget that all of these change the value of dot.
  87. You can find out what dot is at any time by typing
  88. .X1
  89. .li
  90. \fB.\fR=
  91. .X2
  92. .ul
  93. Ed
  94. will respond by printing the value of dot.
  95. .PG
  96. Let's summarize some things about the ``p'' command
  97. and dot.
  98. Essentially ``p'' can be preceded by 0, 1, or 2 line numbers.
  99. If there is no line number given, it prints the ``current line'',
  100. the line that dot refers to.
  101. If there is one line number given
  102. (with or without the letter ``p''),
  103. it prints that line (and dot is set there); and if there
  104. are two line numbers, it prints all the lines in that range
  105. (and sets dot to the last line printed.)
  106. If two line numbers are specified
  107. the first can't be bigger than the second (see Exercise 2.)
  108. .PG
  109. Typing a single newline will cause printing of the next line \(mi
  110. it's
  111. equivalent to ``\*.+1p''.
  112. Try it.
  113. Try typing ``^'' \(mi it's equivalent to
  114. ``\*.\(mi1p''.
  115. .H1
  116. Deleting lines: the ``d'' command
  117. .H2
  118. .PG
  119. Suppose we want to get rid of the three extra lines in the buffer.
  120. This is done by the
  121. .ul
  122. delete
  123. command
  124. .X1
  125. d
  126. .X2
  127. Except that ``d'' deletes lines instead of printing them,
  128. its action is similar to that of ``p''.
  129. The lines to be deleted are specified for ``d''
  130. exactly as they are for ``p'':
  131. .X1
  132. \fIstarting line, ending line\fP d
  133. .X2
  134. Thus the command
  135. .X1
  136. 4,$d
  137. .X2
  138. deletes lines 4 through the end.
  139. There are now three lines left, as we can check by using
  140. .X1
  141. 1,$p
  142. .X2
  143. And notice that ``$'' now is line 3!
  144. Dot
  145. is set to the next line after the last line deleted,
  146. unless the last line deleted is the last line in the buffer.
  147. In that case, dot is set to ``$''.
  148. .H1
  149. Exercise 4:
  150. .H2
  151. .PG
  152. Experiment with ``a'', ``e'', ``r'', ``w'', ``p'', and ``d''
  153. until you are sure that you
  154. know what they do, and until you understand how dot, ``$'', and
  155. line numbers are used.
  156. .PG
  157. If you are adventurous, try using line numbers with ``a'', ``r'', and
  158. ``w'' as well.
  159. You will find that ``a'' will append lines
  160. .ul
  161. after
  162. the line number that you specify (rather than after dot); that ``r'' reads
  163. a file in
  164. .ul
  165. after
  166. the line number you specify (not necessarily
  167. at the end of the buffer); and that ``w'' will write out exactly the lines
  168. you specify, not necessarily the whole buffer.
  169. These variations are sometimes handy.
  170. For instance you can insert a file at the beginning of a buffer
  171. by saying
  172. .X1
  173. 0r filename
  174. .X2
  175. and you can enter lines at the beginning of the buffer
  176. by saying
  177. .X1
  178. 0a
  179. .li
  180. . . . \fItext\fP . . .
  181. .li
  182. \fB.\fR
  183. .X2
  184. Notice that
  185. ``\*.w'' is
  186. .ul
  187. very
  188. different from
  189. .X1
  190. .li
  191. \fB.\fR
  192. w
  193. .X2
  194. .H1
  195. Modifying text: the Substitute command ``s''
  196. .H2
  197. .PG
  198. We are now ready to try one of the most important
  199. of all commands \(mi the substitute command
  200. .X1
  201. s
  202. .X2
  203. This is the command
  204. that is used to change individual
  205. words or letters within a line or group of lines.
  206. It is what we use, for example, for correcting spelling
  207. mistakes and typing errors.
  208. .PG
  209. Suppose that by a typing error, line 1 says
  210. .X1
  211. Now is th time
  212. .X2
  213. \(mi the ``e'' has been left off ``the''.
  214. We can use ``s'' to fix this up as follows:
  215. .X1
  216. 1s/th/the/
  217. .X2
  218. This says: ``in line 1, substitute for the characters `th'
  219. the characters `the'.''
  220. To verify
  221. that it works (\fIed\fR will not print
  222. the result automatically) we say
  223. .X1
  224. p
  225. .X2
  226. and get
  227. .X1
  228. Now is the time
  229. .X2
  230. which is what we wanted.
  231. Notice that dot must have been set to the line
  232. where the substitution took place, since the ``p'' command
  233. printed that line.
  234. Dot is always set this way with the ``s'' command.
  235. .PG
  236. The general way to use the substitute command is
  237. .X1
  238. \fIstarting-line, ending-line\fP s/\fIchange this\fP/\fIto this\fP/
  239. .X2
  240. Whatever string of characters is between the first pair of
  241. slashes is replaced by whatever is between the second pair,
  242. in
  243. .ul
  244. all
  245. the lines between starting line and ending line.
  246. Only the first occurrence on each line is changed, however.
  247. If you want to change
  248. .ul
  249. every
  250. occurrence, see Exercise 5.
  251. The rules for line numbers are the same as those for ``p'',
  252. except that dot is set to the last line changed.
  253. (But there is a trap for the unwary: if no substitution
  254. took place, dot is
  255. .ul
  256. not
  257. changed.
  258. This causes an error ``?'' as a warning.)
  259. .PG
  260. Thus we can say
  261. .X1
  262. 1,$s/speling/spelling/
  263. .X2
  264. and correct the first spelling mistake
  265. on each line
  266. in the text.
  267. (This is useful for people who are consistent
  268. misspellers!)
  269. .PG
  270. If no line numbers are given, the ``s'' command assumes we mean
  271. ``make the substitution on line dot'', so it changes things only
  272. on the current line.
  273. This leads to the very common sequence
  274. .X1
  275. s/something/something else/p
  276. .X2
  277. which makes some correction on the
  278. current line, and then prints it, to make sure it
  279. worked out right.
  280. If it didn't,
  281. we can try again.
  282. (Notice that we put
  283. a print command on the same line as the substitute.
  284. With few exceptions, ``p'' can follow any command;
  285. no other multi-command lines are legal.)
  286. .PG
  287. It's also legal to say
  288. .X1
  289. s/ . . . //
  290. .X2
  291. which means ``change the first
  292. string of characters to
  293. \fInothing\fR'',
  294. i.e.,
  295. remove them.
  296. This is useful for deleting extra words in a line or removing extra
  297. letters from words.
  298. For instance, if we had
  299. .X1
  300. Nowxx is the time
  301. .X2
  302. we can say
  303. .X1
  304. s/xx//p
  305. .X2
  306. to get
  307. .X1
  308. Now is the time
  309. .X2
  310. Notice that ``//'' here means ``no characters'', not a blank.
  311. There
  312. .ul
  313. is
  314. a difference!
  315. (See below for another meaning of ``//''.)
  316.