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

  1. .H1
  2. Special Characters
  3. .H2
  4. .PG
  5. You may have noticed that things just don't work right when you used
  6. some characters like ``.'', ``*'', ``$'', and others in
  7. context searches and the substitute command.
  8. The reason is rather complex, although the cure is simple.
  9. Basically,
  10. .ul
  11. ed
  12. treats these characters as special, with special meanings.
  13. For instance,
  14. .ul
  15. in a context search or the first string of the substitute command only,
  16. \*. means ``any character,'' not a period, so
  17. .X1
  18. /x\*.y/
  19. .X2
  20. means ``a line with an  x,
  21. .ul
  22. any character,
  23. and a y,''
  24. .ul
  25. not
  26. just ``a line with an x, a period, and a y.''~
  27. A complete list of the special characters
  28. that can cause trouble is the following:
  29. .X1
  30. ^    \*.    $    [    *    \\
  31. .X2
  32. .ul
  33. Warning:
  34. The backslash character \\ is special to
  35. .ul
  36. ed.
  37. For safety's sake, 
  38. avoid it where possible.
  39. If you have to use one of the special characters
  40. in a substitute command,
  41. you can turn off its magic meaning temporarily
  42. by preceding it with the backslash.
  43. Thus
  44. .X1
  45. s/\\\\\\\*.\\*/backslash dot star/
  46. .X2
  47. will change ``\\\*.*'' into ``backslash dot star''.
  48. .PG
  49. Here is a hurried synopsis of the other special characters.
  50. First, the circumflex `` ^ ''
  51. signifies
  52. the beginning of a line.
  53. Thus
  54. .X1
  55. /^string/
  56. .X2
  57. finds ``string'' only if it is at the beginning of a line:
  58. it will find
  59. .X1
  60. string
  61. .X2
  62. but not
  63. .X1
  64. the string...
  65. .X2
  66. The dollar-sign ``$'' is just the opposite of the circumflex;
  67. it means the end of a line:
  68. .X1
  69. /string$/
  70. .X2
  71. will only find an occurrence of ``string''
  72. that is at the end of some line.
  73. This implies, of course,
  74. that
  75. .X1
  76. /^string$/
  77. .X2
  78. will find only a line that contains just ``string'',
  79. and
  80. .X1
  81. /^\*.$/
  82. .X2
  83. finds a line containing exactly one character.
  84. .PG
  85. The character ``\*.'', as we mentioned above,
  86. matches anything;
  87. .X1
  88. /x\*.y/
  89. .X2
  90. matches any of
  91. .X1
  92. x+y
  93. x-y
  94. x y
  95. x\*.y
  96. .X2
  97. This is useful in conjunction with ``*'',
  98. which is a repetition character;
  99. ``a*'' is a shorthand for ``any number of a's,''
  100. so ``\*.*'' matches any number of anythings.
  101. This is used like this:
  102. .X1
  103. s/\*.*/stuff/
  104. .X2
  105. which changes an entire line,
  106. or
  107. .X1
  108. s/\*.*,//
  109. .X2
  110. which deletes all characters in the line up to and
  111. including the last comma.
  112. (Since
  113. ``\*.*'' finds the longest possible match,
  114. this goes up to the last comma.)
  115. .PG
  116. ``['' is used with ``]'' to form ``character classes'';
  117. for example,
  118. .X1
  119. /[1234567890]/
  120. .X2
  121. matches any single digit _
  122. any one of the characters inside the braces
  123. will cause a match.
  124. .PG
  125. Finally, the ``&'' is another shorthand character -
  126. it is used only on the right-hand part of a substitute command
  127. where it means ``whatever was matched on the left-hand side''.
  128. It is used to save typing.
  129. Suppose the current line contained
  130. .X1
  131. Now is the time
  132. .X2
  133. and we wanted to put parentheses around it.
  134. We could just retype the line, but
  135. this is tedious.
  136. Or we could say
  137. .X1
  138. s/^/(/
  139. s/$/)/
  140. .X2
  141. using our knowledge of ``^'' and ``$''.
  142. But the easiest way uses the ``&'':
  143. .X1
  144. s/\*.*/(&)/
  145. .X2
  146. This says ``match the whole line, and replace it
  147. by itself surrounded by parens.''~
  148. The ``&'' can be used several times in a line;
  149. consider
  150. using
  151. .X1
  152. s/\*.*/&?  &!!/
  153. .X2
  154. to produce
  155. .X1
  156. Now is the time?  Now is the time!!
  157. .X2
  158. .PG
  159. We don't have to match the whole line, of course:
  160. if the buffer contains
  161. .X1
  162. the end of the world
  163. .X2
  164. we could type
  165. .X1
  166. /world/s//& is at hand/
  167. .X2
  168. to produce
  169. .X1
  170. the end of the world is at hand
  171. .X2
  172. Observe this expression carefully,
  173. for it illustrates how to take advantage of
  174. .ul
  175. ed
  176. to save typing.
  177. The string ``/world/''
  178. found the desired line;
  179. the shorthand ``//'' found the same
  180. word in the line;
  181. and the ``&'' saved us from typing it again.
  182. .PG
  183. The ``&'' is a special character only within
  184. the replacement text of a substitute command,
  185. and has no special meaning elsewhere.
  186. We can turn off the special meaning of ``&''
  187. by preceding it with a ``\\'':
  188. .X1
  189. s/ampersand/\\&/
  190. .X2
  191. will convert the word ``ampersand'' into the literal symbol
  192. ``&''
  193. in the current line.
  194.