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