nvi: Bring in version 2.1.3 (update from 2.1.1)
[dragonfly.git] / contrib / nvi / docs / tutorial / vi.beginner
1 Section 1: {^F} {ZZ}
2
3 To get out of this tutorial, type: ZZ (two capital Z's).
4
5 Learning a new computer system implies learning a new text editor.  These
6 tutorial lessons were created by Dain Samples to help you come to grips with
7 UC Berkeley's screen oriented editor called vi (for VIsual). This tutorial
8 uses the vi editor itself as the means of presentation. 
9
10 For best use of this tutorial, read all of a screen before performing any of
11 the indicated actions.  This tutorial (or, at least, the first half of it) has
12 been designed to systematically present the vi commands IF THE INSTRUCTIONS
13 ARE FOLLOWED!  If you are too adventuresome, you may find yourself lost.  If
14 you ever find yourself stuck, remember the first line of this section.
15
16 OK, now find the control key on your keyboard; it usually has CTL or CTRL
17 written on its upper surface.  Your first assignment is to hold the control
18 key down while you press the 'F' key on your keyboard.  Please do so now.
19
20
21
22 Section 2: {^F} {^B}
23 Many of vi's commands use the control key and some other key in combination,
24 as with the control and the 'F' key above.  This is abbreviated CTL-F, or ^F.
25
26 As you have probably guessed by now, ^F (CTL-F) moves you forward a fixed
27 number of lines in the file.  Throughout the remainder of the tutorial when
28 you are ready to advance to the next section of text, hit ^F.
29
30 The opposite command is ^B.  Just for fun, you might want to try a ^B to see
31 the previous section again.  Be sure to do a ^F to return you here.
32
33 Determine what the cursor looks like on your screen.  Whatever it is (a box,
34 an underscore, blinking, flashing, inverse, etc.) it should now be positioned
35 in the upper left-hand corner of your screen under or on the S of Section.
36 Become familiar with your cursor: to use vi correctly it is important to
37 always know where the cursor is.
38
39 Did you notice that when you do a ^F the cursor is left at the top of the
40 screen, and a ^B leaves the cursor near the bottom of the screen?  Try the two
41 commands ^B^F again.  And now do another ^F to see the next section.
42
43 Section 3: {^F} {^B}
44 You now have two basic commands for examining a file, both forwards (^F) and
45 backwards (^B).
46
47 Note that these are vi text editing commands: they are not commands for the
48 tutorial.  Indeed, this tutorial is nothing but a text file which you are now
49 editing.  Everything you do and learn in this tutorial will be applicable to
50 editing text files.
51
52 Therefore, when you are editing a file and are ready to see more of the text,
53 entering ^F will get you to the next section of the file.  Entering ^B will
54 show you the previous section.
55
56 Time for you to do another ^F.
57
58
59
60
61
62
63
64 Section 4: {^F} {^B} {^M} (return key)
65 We will adopt the notation of putting commands in curly braces so we can write
66 them unambiguously.  For example, if you are to type the command sequence
67 "control B control F" (as we asked you to do above) it would appear as {^B^F}.
68 This allows clear delineation of the command strings from the text. Remember
69 that the curly braces are NOT part of the command string you are to type. Do
70 NOT type the curly braces.  
71
72 Sometimes, the command string in the curly braces will be rather long, and may
73 be such that the first couple of characters of the command will erase from
74 the screen the string you are trying to read and type.  It is suggested that
75 you write down the longer commands BEFORE you type them so you won't forget
76 them once they disappear.
77
78 Now locate the return key on your keyboard: it is usually marked 'RETURN',
79 indicate hitting the return key.  In fact, the control-M key sequence is
80 exactly the same as if you hit the return key, and vice versa.
81
82 Now type {^F}.
83
84
85 Section 5: {:q!} {ZZ} {^M} (return key)
86 Recognize that this tutorial is nothing more than a text file that you
87 are editing.  This means that if you do something wrong, it is possible
88 for you to destroy the information in this file.  Don't worry.  If this
89 happens, type {ZZ} (two capital Z's) or {:q!^M} to leave the tutorial.
90 Restart the tutorial.  Once in the tutorial, you can then page forward
91 with {^F} until you are back to where you want to be.  (There are
92 easier ways to do this, some of which will be discussed later, but this
93 is the most straightforward.)
94
95 You may want to write these commands down in a convenient place for quick
96 reference: {:q!^M} and {ZZ}
97
98 We will assume that you now know to do a {^F} to advance the file
99
100
101
102
103
104
105
106 Section 6: {m} {G} {'} {z}
107 Now that you know how to get around in the file via ^F and ^B let's look at
108 other ways of examining a text file.  Sometimes it is necessary, in the midst
109 of editing a file, to examine another part of the file.  You are then faced
110 with the problem of remembering your place in the file, looking at the other
111 text, and then getting back to your original location.  Vi has a 'mark'
112 command, m. Type {mp}.  You have just 'marked' your current location in the
113 file and given it the name 'p'.  The command string below will do three
114 things: position you at the beginning of the file (line 1), then return you to
115 the location 'p' that you just marked with the 'm' command, and, since the
116 screen will not look exactly the same as it does right now, the 'z' command
117 will reposition the screen. (You may want to write the string down before
118 typing it: once you type {1G} it will no longer be on the screen.)
119
120 So now type {1G'pz^M} - a one followed by a capital G, followed by the quote
121 mark, followed by a lower case 'p', then a lower case 'z', then a return
122 (which is the same as a ^M).  The {1G} moves you to line 1, i.e. the beginning
123 of the file.  The {'p} moves you to the location you marked with {mp}.  The
124 {z^M} command will repaint the screen putting the cursor at the top of the
125 screen. (Now {^F}.)
126
127 Section 7: {m} {G} {'} {z}
128 Let's look at some variations on those commands.  If you wanted to look at
129 line 22 in the file and return to this location you could type {mp22G'p}.  Do
130 so now, observing that {22G} puts your cursor at the beginning of section 2 in
131 the middle of the screen.
132
133 Also note that, without the {z^M} command, the line with 'Section 7' on it is
134 now in the MIDDLE of the screen, and not at the top.  Our cursor is on the
135 correct line (where we did the {mp} command) but the line is not where we
136 might like it to be on the screen.  That is the function of the {z^M} command.
137 (Remember, ^M is the same as the 'return' key on your keyboard.)  Type {z^M}
138 now and observe the effect.
139
140 As you can see, the 'Section 7' line is now at the top of the screen with the
141 cursor happily under the capital S.  If you would like the cursor line (i.e.
142 the line which the cursor is on) in the middle of the screen again, you would
143 type {z.}.  If you wanted the cursor line to be at the BOTTOM of the screen,
144 type {z-}.  Try typing {z-z.z^M} and watch what happens.
145
146 {^F}
147
148 Section 8: {z} {m} {'}
149
150 Note that the z command does not change the position of our cursor in the file
151 itself, it simply moves the cursor around on the screen by moving the contents
152 of the file around on the screen.  The cursor stays on the same line of the
153 file when using the z command.
154
155 This brings up an important point.  There are two questions that the users of
156 vi continually need to know the answer to: "Where am I in the file?" and
157 "Where am I on the screen?"  The cursor on your terminal shows the answer to
158 both questions.  Some commands will move you around in the file, usually
159 changing the location of the cursor on the screen as well.  Other commands
160 move the cursor around on the screen without changing your location in the
161 file.
162
163 Now type {ma}.  Your location in the file has been given the name 'a'. If you
164 type {'p'a} you will see the previous location we marked in section 7, and
165 then will be returned to the current location.  (You will want to do a {z^M}
166 to repaint the screen afterwards.)  Try it.  
167 {^F}
168
169 Section 9: {m} {''}
170 Now we can move about in our file pretty freely.  By using the {m} command we
171 can give the current cursor position a lower-case-character name, like 'p',
172 'a', 'e', 'm', or 'b'.  Using the {G} command preceded by a line number we can
173 look at any line in the file we like.  Using the single quote command {'}
174 followed by a character used in an {m} command, we can return to any location
175 in the file we have marked.
176
177 However, try {m3}, or {mM}.  You should hear a beep, or bell.  Only lower-case
178 letters are acceptable to the {m} and {'} commands: numbers, upper-case
179 letters, and special characters are not acceptable.
180
181 If you type the {'} command with a character that is lower-case alphabetic but
182 that has not been used in an {m} command, or for which the 'marked' text has
183 been deleted, you will also get a beep.  Try {'i}.  You should get a beep
184 because the command {mi} has never been issued.  (Unless you've been
185 experimenting.)
186
187 The command {''} attempts to return you to the location at which you last
188 modified some part of your file.  However, my experience has been that it is
189 difficult to predict exactly where you will end up.  
190 Section 10: {^M} {-}
191 Now do {ma}, marking your position at the top of the screen.  Now hit {^M} (or
192 return) until the cursor is right ... 
193 * <- here, over/under the asterisk.  Now
194 type {mb'a'b} and watch the cursor move from the asterisk to the top of the
195 screen and back again.
196
197 The {^M} command moves the cursor to the beginning of the next line.  Now type
198 {^M} until the cursor is right ...
199 * <- here.  The command to move the cursor to the beginning of the
200 previous line is {-}.  Practice moving the cursor around on the screen by using
201 {^M} and {-}.  BE CAREFUL to not move the cursor OFF the screen just yet.  If
202 you do, type {'az^M}.
203
204 Now we can move to any line within the screen.  Practice moving around in the
205 file using the {^F}, {^B}, {-}, {^M}, {z}, and {'} commands.  When you are
206 fairly confident that you can get to where you need to be in the file, and
207 position the cursor on the screen where you want it type {'az^M^F} (which, of
208 course, moves you back to the beginning of this section, repositions the
209 cursor at the top of the screen, and advances you to the next section).
210
211 Section 11: scrolling: {^M}
212 The cursor should now be on the S of 'Section 11', and this should be on the
213 first line of the screen.  If it is not, do {^M} or {-} as appropriate to put
214 the cursor on the section line, and type {z^M}.
215
216 Type {mc} to mark your place.
217
218 Now type {^M} until the cursor is on the last line of this screen.  Now do one
219 more {^M} and observe the result.  This is called scrolling.  When you
220 attempted to move to a line not displayed on the screen, the line at the top of
221 the screen was 'scrolled off', and a line at the bottom of the screen was
222 'scrolled on'.  The top line with 'Section 11' should no longer be visible.
223
224 Now type {'cz^M} to reset the screen and type {^F} for the next section.
225
226
227
228
229
230
231
232 Section 12: {-} {z}
233
234 The {-} command moves the cursor to the previous line in the file.  Now type
235 {-}, which attempts to move the cursor to the previous line in this file.
236 However, that line is not on the screen.  The resulting action will depend on
237 your terminal.  (Do a {^Mz^M} to reposition the file).  On intelligent
238 terminals (e.g. VT100s, Z19s, Concept 100s), a top line is 'scrolled on' and
239 the bottom line is 'scrolled off'.  Other terminals, however, may not have
240 this 'reverse scrolling' feature.  They will simply repaint the screen with
241 the cursor line in the middle of the screen.  On such terminals it is
242 necessary to type {z^M} to get the cursor line back to the top of the screen.
243
244
245
246
247
248
249
250
251
252
253 Section 13:
254 Up until this point, the tutorial has always tried to make sure that the first
255 line of each screen has on it the section number and a list of the commands
256 covered in that section.  This will no longer be strictly maintained.  If you
257 want the section line at the top of the screen, you now know enough commands to
258 do it easily: do {^M} or {-} until the cursor is on the section line and
259 then {z^M}.  Also, from this point on, it may not be the case that a {^F} will
260 put you at the beginning of the next section.  Therefore, be aware of where you
261 are in the file as we look at other commands.  You may have to find your way
262 back to a particular section without any help from the tutorial.  If you do not
263 feel comfortable with this, then it is suggested that you practice moving from
264 section 1 to section 13, back and forth, using {^M}, {-}, {^F}, and {^B}
265 commands for a while.
266
267 Also make liberal use of the mark command {m}: if, for example, you make a
268 habit of using {mz} to mark your current location in the file, then you will
269 always be able to return to that location with {'z} if the editor does
270 something strange and you have no idea where you are or what happened.
271
272 And finally, the proscription against experimentation is hereby lifted: play
273 with the editor.  Feel free to try out variations on the commands and move
274 around in the file.  By this time you should be able to recover from any gross
275 errors.
276
277 Section 14: {^E} {^Y} {^D} {^U}
278 Let us now look at a few other commands for moving around in the file, and
279 moving the file around on the screen.  Note that the commands we have already
280 looked at are sufficient: you really don't need any more commands for looking
281 in a file.  The following commands are not absolutely necessary.  However,
282 they can make editing more convenient, and you should take note of their
283 existence.  But it would be perfectly valid to decide to ignore them on this
284 first pass: you can learn them later when you see a need for them, if you ever
285 do.
286
287 First, let's clear up some potentially confusing language.  In at least one
288 place in the official document ('An Introduction to Display Editing with Vi'
289 by William Joy, and Mark Horton, September 1980), the expression "to scroll
290 down text" means that the cursor is moved down in your file.  However, note
291 that this may result in the text on the screen moving UP.  This use of the
292 word 'scroll' refers to the action of the cursor within the file.  However,
293 another legitimate use of the word refers to the action of the text on the
294 screen.  That is, if the lines on your screen move up toward the top of the
295 screen, this would be 'scrolling the screen up'.  If the lines move down
296 toward the bottom of the screen, this would be refered to as scrolling down.
297
298 I have tried to maintain the following jargon: 'scrolling' refers to what the
299 text does on the screen, not to what the cursor does within the file.  For the
300 latter I will refer to the cursor 'moving', or to 'moving the cursor'.  I
301 realize that this is not necessarily consistent with Joy and Horton, but they
302 were wrong.
303
304 {^E} scrolls the whole screen up one line, keeping the cursor on the same line,
305 if possible.  However, if the cursor line is the first line on the screen, then
306 the cursor is moved to the next line in the file.  Try typing {^E}.
307
308 {^Y} scrolls the screen down one line, keeping the cursor on the same line, if
309 possible.  However, if the cursor line is the last line on the screen, then the
310 cursor is moved to the previous line in the file.  Try it.
311
312 {^D} moves the cursor down into the file, scrolling the screen up.
313
314 {^U} moves the cursor up into the file, also scrolling the screen if the
315 terminal you are on has the reverse scroll capability.  Otherwise the
316 screen is repainted.
317
318 Note that {^E} and {^Y} move the cursor on the screen while trying to keep the
319 cursor at the same place in the file (if possible: however, the cursor can
320 never move off screen), while {^D} and {^U} keep the cursor at the same place
321 on the screen while moving the cursor within the file.
322
323 Section 15: {/ .. /^M}
324
325 Another way to position yourself in the file is by giving the editor a string
326 to search for.  Type the following: {/Here 1/^M} and the cursor should end up
327 right ...........................here ^.  Now type {/Section 15:/^M} and the
328 cursor will end up over/on .....................here ^.  Now type {//^M} and
329 observe that the cursor is now over the capital S five lines above this line.
330 Typing {//^M} several more times will bounce the cursor back and forth between
331 the two occurrences of the string.  In other words, when you type a string
332 between the two slashes, it is searched for.  Typing the slashes with nothing
333 between them acts as if you had typed the previous string again.
334
335 Observe that the string you type between the two slashes is entered on the
336 bottom line of the screen.  Now type {/Search for x /^M} except replace the 'x'
337 in the string with some other character, say 'b'.  The message "Pattern not
338 found" should appear on the bottom of the screen.  If you hadn't replaced the
339 'x', then you would have found the string.  Try it.
340
341 Section 16: {? .. ?^M} {n} (search strings: ^ $)
342
343 When you surround the sought-for string with slashes as in {/Search/}, the
344 file is searched beginning from your current position in the file.  If the
345 string is not found by the end of the file, searching is restarted at the
346 beginning of the file.  However, if you do want the search to find the
347 PREVIOUS rather than the NEXT occurrence of the string, surround the string
348 with question marks instead of slash marks.
349
350 Below are several occurrences of the same string.  
351 Here 2            Here 2 Here 2
352  Here 2             Here 2.
353 Observe the effect of the following search commands (try them in the
354 sequence shown):
355 {/Here 2/^M}  {//^M}  {??^M}
356 {/^Here 2/^M}  {//^M}  {??^M}
357 {/Here 2$/^M}  {//^M}  {??^M}
358
359 The first command looks for the next occurrence of the string 'Here 2'.
360 However the second line of commands looks for an occurrence of 'Here 2' that
361 is at the beginning of the line.  When the up-arrow is the first character of
362 a search string it stands for the beginning of the line.  When the dollar-sign
363 is the last character of the search string it stands for the end of the line.
364 Therefore, the third line of commands searches for the string only when it is
365 at the end of the line.  Since there is only one place the string begins a
366 line, and only one place the string ends the line, subsequent {//^M} and
367 {??^M} will find those same strings over and over.
368
369 The {n} command will find the next occurrence of the / or ? search
370 string.  Try {/Here 2/^M} followed by several {n} and observe the
371 effect.  Then try {??^M} followed by several {n}.  The {n} command
372 remembers the direction of the last search.  It is just a way to save a
373 few keystrokes.
374
375 Section 17: \ and magic-characters in search strings
376
377 Now type {/Here 3$/^M}.  You might expect the cursor to end up
378 right......^ here.  However, you will get "Pattern not found" at the bottom of
379 the screen.  Remember that the dollar-sign stands for the end of the line.
380 Somehow, you must tell vi that you do not want the end of the line, but a
381 dollar-sign.  In other words, you must take away the special meaning that the
382 dollar-sign has for the search mechanism.  You do this (for any special
383 character, including the up-arrow ^) by putting a back-slash ('\', not '/') in
384 front of the character.
385
386 Now try {/Here 3\$/^M} and you should end up nine lines above this one.  Try
387 {//^M} and note that it returns you to the same place, and not to the first
388 line of this paragraph: the back-slash character is not part of the search
389 string and will not be found.  To find the string in the first line of this
390 paragraph, type {/Here 3\\\$/^M}.  There are three back-slashes: the first takes
391 away the special meaning from the second, and the third takes away the special
392 meaning from the dollar-sign.
393
394 Following is a list of the characters that have special meanings in search
395 strings.  If you wish to find a string containing one of these characters, you
396 will have to be precede the character with a backslash.  These characters are
397 called magic characters because of the fun and games you can have with them
398 and they can have with you, if you aren't aware of what they do.  
399
400   ^ - (up-arrow)       beginning of a line
401   $ - (dollar-sign)    end of a line
402   . - (period)         matches any character
403   \ - (backslant)      the escape character itself
404   [ - (square bracket) for finding patterns (see section #SEARCH)
405   ] - (square bracket) ditto
406   * - (asterisk)       ditto
407
408 Without trying to explain it here, note that {:set nomagic^M} turns off the
409 special meanings of all but the ^ up-arrow, $ dollar-sign, and backslash
410 characters.
411
412 Section 18: {: (colon commands)} {ZZ}
413
414 In this section we will discuss getting into and out of the editor in more
415 detail.  If you are editing a file and wish to save the results the command
416 sequence {:w^M} writes the current contents of the file out to disk, using the
417 file name you used when you invoked the editor.  That is, if you are at the
418 command level in Unix, and you invoke vi with {vi foo} where foo is the name
419 of the file you wish to edit, then foo is the name of the file used by the
420 {:w^M} command.
421
422 If you are done, the write and quit commands can be combined into a single
423 command {:wq^M}.  An even simpler way is the command {ZZ} (two capital Z's).
424
425 If, for some reason, you wish to exit without saving any changes you have made,
426 {:q!^M} does the trick.  If you have not made any changes, the exclamation
427 point is not necessary: {:q^M}.  Vi is pretty good about not letting you
428 get out without warning you that you haven't saved your file.
429
430 We have mentioned before that you are currently in the vi editor, editing a
431 file.  If you wish to start the tutorial over from the very beginning, you
432 could {ZZ}, and then type {vi.tut beginner} in response to the Unix prompt.
433 This will create a fresh copy of this file for you, which might be necessary 
434 if you accidentally destroyed the copy you were working with.  Just do a 
435 search for the last section you were in: e.g.  {/Section 18:/^Mz^M}.
436
437 Section 19: {H} {M} {L}
438
439 Here are a few more commands that will move you around on the screen.  Again,
440 they are not absolutely necessary, but they can make screen positioning easier:
441
442 {H} - puts the cursor at the top of the screen (the 'home' position)
443
444 {M} - puts the cursor in the middle of the screen
445
446 {L} - puts the cursor at the bottom of the screen.
447
448 Try typing {HML} and watch the cursor.
449
450 Try typing {5HM5L} and note that 5H puts you five lines from the top of the
451 screen, and 5L puts you five lines from the bottom of the screen.
452
453 Section 20: {w} {b} {0} {W} {B} {e} {E} {'} {`}
454
455 Up to this point we have concentrated on positioning in the file, and
456 positioning on the screen.  Now let's look at positioning in a line.  Put the
457 cursor at the beginning of the following line and type {z^M}:
458
459 This is a test line: your cursor should initially be at its beginning.
460
461 The test line should now be at the top of your screen. Type {w} several times.
462 Note that it moves you forward to the beginning of the next word.  Now type
463 {b} (back to the beginning of the word) several times till you are at the
464 beginning of the line.  (If you accidentally type too many {b}, type {w} until
465 you are on the beginning of the line again.) Type {wwwww} (five w's) and note
466 that the cursor is now on the colon in the sentence.  The lower-case w command
467 moves you forward one word, paying attention to certain characters such as
468 colon and period as delimiters and counting them as words themselves.  Now
469 type {0} (zero, not o 'oh'): this moves you to the beginning of the current
470 line.  Now type {5w} and notice that this has the effect of repeating {w} five
471 times and that you are now back on the colon.  Type {0} (zero) again.  To
472 ignore the delimiters and to move to the beginning of the next word using only
473 blanks, tabs and carriage-returns (these are called white-space characters) to
474 delimit the words, use the {W} command: upper-case W.  {B} takes you back a
475 word using white-space characters as word delimiters.
476
477 Note that the commands {wbWB} do not stop at the beginning or end of a line:
478 they will continue to the next word on the next line in the direction specified
479 (a blank line counts as a word).
480
481 If you are interested in the END of the word, and not the BEGINNING, then use
482 the {e} and {E} commands.  These commands only move forward and there are no
483 corresponding 'reverse search' commands for the end of a word.
484
485 Also, we have been using the {'} command to move the cursor to a position that
486 we have previously marked with the {m} command.  However, position the cursor
487 in the middle of a line (any line, just pick one) and type {mk}, marking that
488 position with the letter k.  Now type a few returns {^M} and type {'k}.
489 Observe that the cursor is now at the beginning of the line that you marked.
490 Now try {`k}: note that this is the reverse apostrophe, or back-quote, or grave
491 accent, or whatever you want to call it.  Also note that it moves you to the
492 character that was marked, not just to the line that was marked.
493
494 In addition, the {``} command works just like the {''} command except that you
495 are taken to the exact character, not just to the line.  (I'm still not
496 sure which exact character, just as I'm still not sure which line.)
497
498 Section 21: {l} {k} {j} {h}
499
500 There are several commands to move around on the screen on a character by
501 character basis:
502
503 l - moves the cursor one character to the RIGHT
504 k - moves the cursor UP one line
505 j - moves the cursor DOWN one line
506 h - moves the cursor one character to the LEFT
507
508 Section 22: {i} {a} {I} {A} {o} {O} ^[ (escape key)
509
510 For this and following sections you will need to use the ESCAPE key on your
511 terminal.  It is usually marked ESC.  Since the escape key is the same as
512 typing {^[} we will use ^[ for the escape key.
513
514 Probably the most often used command in an editor is the insert command.  Below
515 are two lines of text, the first correct, the second incorrect.  Position your
516 cursor at the beginning of Line 1 and type {z^M}.
517
518 Line 1: This is an example of the insert command.
519 Line 2: This is an of the insert command.
520
521 To make line 2 look like line 1, we are going to insert the characters
522 'example ' before the word 'of'.  So, now move the cursor so that it is
523 positioned on the 'o' of 'of'.  (You can do this by typing {^M} to move
524 to the beginning of line 2, followed by {6w} or {wwwwww} to position the cursor
525 on the word 'of'.)
526
527 Now carefully type the following string and observe the effects:
528   {iexample ^[}  (remember: ^[ is the escape key)}
529 The {i} begins the insert mode, and 'example ' is inserted into the line: 
530 be sure to notice the blank in 'example '.  The ^[ ends insertion mode, 
531 and the line is updated to include the new string.  Line 1 should look exactly 
532 like Line 2.
533
534 Move the cursor to the beginning of Line 3 below and type {z^M}:
535
536 Line 3: These lines are examples for the 'a' command.
537 Line 4: These line are examples for the '
538
539 We will change line four to look like line three by using the append command.
540 We need to append an 's' to the word 'line'.  Position the cursor on the 'e'
541 of 'line'.  You can do this in several ways, one way is the following:
542 First, type {/line /^M}.  This puts us on the word 'line' in Line 4
543 (the blank in the search string is important!).  Next, type {e}.  The 'e' puts
544 us at the end of the word.  Now, type {as^[  (^[ is the escape character)}.  
545 The 'a' puts us in insert mode, AFTER the current character.  We appended the 
546 's', and the escape ^[ ended the insert mode.
547
548 The difference between {i} (insert) and {a} (append) is that {i} begins
549 inserting text BEFORE the cursor, and {a} begins inserting AFTER the cursor.
550
551 Now type {Aa' command.^[}.  The cursor is moved to the end of the line and the
552 string following {A} is inserted into the text.  Line 4 should now look like
553 line 3.
554
555 Just as {A} moves you to the end of the line to begin inserting, {I} would
556 begin inserting at the FRONT of the line.
557
558 To begin the insertion of a line after the cursor line, type {o}.  To insert a
559 line before the cursor line, type {O}.  In other words {o123^[} is equivalent
560 to {A^M123^[}, and {O123^[} is equivalent to {I123^M^[}.  The text after the
561 {o} or {O} is ended with an escape ^[.
562
563 This paragraph contains information that is terminal dependent: you will just
564 have to experiment to discover what your terminal does.  Once in the insert
565 mode, if you make a mistake in the typing, ^H will delete the previous
566 character up to the beginning of the current insertion.  ^W will delete the
567 previous word, and one of ^U, @, or ^X will delete the current line (up to the
568 beginning of the current insertion).  You will need to experiment with ^U, @,
569 and ^X to determine which works for your terminal.
570
571 Section 23: {f} {x} {X} {w} {l} {r} {R} {s} {S} {J}
572
573 Position the cursor at the beginning of line 5 and {z^M}:
574
575 Line 5: The line as it should be.
576 Line 6: The line as it shouldn't be.
577
578 To make Line 6 like Line 5, we have to delete the 'n', the apostrophe, and the
579 't'.  There are several ways to position ourselves at the 'n'.  Choose
580 whichever one suits your fancy:
581
582 {/n't/^M}
583 {^M7w6l}  or  {^M7w6 } (note the space)
584 {^M3fn}  (finds the 3rd 'n' on the line)
585
586 Now {xxx} will delete the three characters, as will {3x}.
587
588 Note that {X} deletes the character just BEFORE the cursor, as opposed
589 to the character AT the cursor.
590
591 Position the cursor at line 7 and {z^M}:
592
593 Line 7: The line as it would be.
594 Line 8: The line as it could be.
595
596 To change line 8 into line 7 we need to change the 'c' in 'could' into a 'w'.
597 The 'r' (replace) command was designed for this.  Typing {rc} is the same as
598 typing {xic^[} (i.e.  delete the 'bad' character and insert the correct
599 new character).  Therefore, assuming that you have positioned the cursor on the
600 'c' of 'could', the easiest way to change 'could' into 'would' is {rw}.
601
602 If you would like to now change the 'would' into 'should', use the substitute
603 command, 's': {ssh^[}.  The difference between 'r' and 's' is that 'r'
604 (replace) replaces the current character with another character, while 's'
605 (substitute) substitutes the current character with a string, ended with an
606 escape.
607
608 The capital letter version of replace {R} replaces each character by a
609 character one at a time until you type an escape, ^[.  The 'S' command
610 substitutes the whole line.
611
612 Position your cursor at the beginning of line 9 and {z^M}.
613
614 Line  9: Love is a many splendored thing.
615 Line 10: Love is a most splendored thing.
616
617 To change line 10 into line 9, position the cursor at the beginning of 'most',
618 and type {Rmany^[}.
619
620 You may have noticed that, when inserting text, a new line is formed by typing
621 {^M}.  When changing, replacing, or substituting text you can make a new line
622 by typing {^M}.  However, neither {x} nor {X} will remove ^M to make two lines 
623 into one line.  To do this, position the cursor on the first of the two lines 
624 you wish to make into a single line and type {J} (uppercase J for 'Join').
625
626 Section 24: {u} {U}
627
628 Finally, before we review, let's look at the undo command.  Position
629 your cursor on line 11 below and {z^M}.
630
631 Line 11: The quick brown fox jumped over the lazy hound dog.
632 Line 12: the qwick black dog dumped over the laxy poune fox.
633
634 Type the following set of commands, and observe carefully the effect of each 
635 of the commands:
636
637 {/^Line 12:/^M} {ft} {rT} {fw} {ru} {w} {Rbrown fox^[} {w} {rj} 
638 {fx} {rz} {w} {Rhound dog^[}
639
640 Line 12 now matches line 11.  Now type {U} - capital 'U'.  And line 12 now
641 looks like it did before you typed in the command strings.  Now type:
642
643 {ft} {rT} {fw} {ru} {^M} {^M}
644
645 and then type {u}:  the cursor jumps back to the line containing the second
646 change you made and 'undoes' it.  That is, {U} 'undoes' all the changes on the
647 line, and {u} 'undoes' only the last change.  Type {u} several times and
648 observe what happens: {u} can undo a previous {u}!
649
650 Caveat: {U} only works as long as the cursor is still on the line.  Move the
651 cursor off the line and {U} will have no effect, except to possibly beep at
652 you.  However, {u} will undo the last change, no matter where it occurred.
653
654 Section 25: review
655
656 At this point, you have all the commands you need in order to make use of vi.
657 The remainder of this tutorial will discuss variations on these commands as
658 well as introduce new commands that make the job of editing more efficient.
659 Here is a brief review of the basic commands we have covered.  They are listed
660 in the order of increasing complexity and/or decreasing necessity (to say that
661 a command is less necessary is not to say that it is less useful!).  These
662 commands allow you to comfortably edit any text file.  There are other
663 commands that will make life easier but will require extra time to learn,
664 obviously.  You may want to consider setting this tutorial aside for several
665 weeks and returning to it later after gaining experience with vi and getting
666 comfortable with it.  The convenience of some of the more exotic commands may
667 then be apparent and worth the extra investment of time and effort
668 required to master them.
669
670 to get into the editor from Unix:           {vi filename}
671 to exit the editor
672       saving all changes                    {ZZ} or {:wq^M}
673       throwing away all changes             {:q!^M}
674       when no changes have been made        {:q^M}
675 save a file without exiting the editor      {:w^M}
676 write the file into another file            {:w filename^M}
677 insert text 
678       before the cursor                     {i ...text... ^[}
679       at the beginning of the line          {I ...text... ^[}
680       after the cursor (append)             {a ...text... ^[}
681       at the end of the line                {A ...text... ^[}
682       after the current line                {o ...text... ^[}
683       before the current line               {O ...text... ^[}
684 delete the character  ...
685       under the cursor                      {x}
686       to the left of the cursor             {X}
687 delete n characters                         {nx} or {nX}  (for n a number)
688 make two lines into one line (Join)         {J}
689 find a string in the file ...
690       searching forward                     {/ ...string... /^M}
691       searching backwards                   {? ...string... ?^M}
692 repeat the last search command              {n}
693 repeat the last search command in the
694   opposite direction                        {N}
695 find the character c on this line ...
696       searching forward                     {fc}
697       searching backward                    {Fc}
698 repeat the last 'find character' command    {;}
699 replace a character with character x        {rx}
700 substitute a single character with text     {s ...text... ^[}
701 substitute n characters with text           {ns ...text... ^[}
702 replace characters one-by-one with text     {R ...text... ^[}
703 undo all changes to the current line        {U}
704 undo the last single change                 {u}
705 move forward in the file a "screenful"      {^F}
706 move back in the file a "screenful"         {^B}
707 move forward in the file one line           {^M} or {+}
708 move backward in the file one line          {-}
709 move to the beginning of the line           {0}
710 move to the end of the line                 {$}
711 move forward one word                       {w}
712 move forward one word, ignoring punctuation {W}
713 move forward to the end of the next word    {e}
714 to the end of the word, ignoring punctuation{E}
715 move backward one word                      {b}
716 move back one word, ignoring punctuation    {B}
717 return to the last line modified            {''}
718 scroll a line onto the top of the screen    {^Y}
719 scroll a line onto the bottom of the screen {^E}
720 move "up" in the file a half-screen         {^U}
721 move "down" in the file a half-screen       {^D}
722 move the cursor to the top screen line      {H}
723 move the cursor to the bottom screen line   {L}
724 move the cursor to the middle line          {M}
725 move LEFT one character position            {h} or {^H}
726 move RIGHT one character position           {l} or { }
727 move UP in the same column                  {k} or {^P}
728 move DOWN in the same column                {j} or {^N}
729 mark the current position, name it x        {mx}
730 move to the line marked/named x             {'x}
731 move to the character position named x      {`x}
732 move to the beginning of the file           {1G}
733 move to the end of the file                 {G}
734 move to line 23 in the file                 {23G}
735 repaint the screen with the cursor line
736        at the top of the screen             {z^M}
737        in the middle of the screen          {z.}
738        at the bottom of the screen          {z-}
739
740 More information on vi can be found in the file vi.advanced, which you can
741 peruse at your leisure.  From UNIX, type {vi.tut advanced^M}.