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