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