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