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