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