Lines Matching refs:the
8 uses the vi editor itself as the means of presentation.
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
14 you ever find yourself stuck, remember the first line of this section.
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.
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.
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.
31 the previous section again. Be sure to do a ^F to return you here.
33 Determine what the cursor looks like on your screen. Whatever it is (a box,
35 in the upper left-hand corner of your screen under or on the S of Section.
37 always know where the cursor is.
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.
47 Note that these are vi text editing commands: they are not commands for the
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.
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
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.
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
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.
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
93 is the most straightforward.)
98 We will assume that you now know to do a {^F} to advance the file
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
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.)
120 So now type {1G'pz^M} - a one followed by a capital G, followed by the quote
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
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.
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.
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,
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.
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
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.
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 {'}
175 in the file we have marked.
177 Try {m3}, {mM}, or {m$}. Not only lower-case letters are acceptable to the
181 If you type the {'} command with a character that that has not been used in an
182 {m} command, or for which the 'marked' text has been deleted, you will get a
183 beep. Try {'i}. You should get a beep because the command {mi} has never
186 The command {''} attempts to return you to the location at which you last
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
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
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).
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}.
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
224 Now type {'cz^M} to reset the screen and type {^F} for the next section.
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
239 on' and the bottom line is 'scrolled off'. Some very old terminals, however,
240 may not have this 'reverse scrolling' feature. They will simply repaint the
241 screen with the cursor line in the middle of the screen. On such terminals it
242 is necessary to type {z^M} to get the cursor line back to the top of the
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
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
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
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
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
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 referred to as scrolling down.
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
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}.
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.
312 {^D} moves the cursor down into the file, scrolling the screen up.
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
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.
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
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.
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.
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
350 Below are several occurrences of the same string.
353 Observe the effect of the following search commands (try them in the
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 caret (circumflex, up-arrow) is the
362 first character of a search string it stands for the beginning of the line.
363 When the dollar-sign is the last character of the search string it stands for
364 the end of the line. Therefore, the third line of commands searches for the
365 string only when it is at the end of the line. Since there is only one place
366 the string begins a line, and only one place the string ends the line,
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
372 remembers the direction of the last search. It is just a way to save a
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 caret ^) by putting a back-slash ('\', not '/') in
384 front of the character.
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.
394 Following is a list of the characters that have special meanings in search
396 will have to precede the character with a backslash. These characters are
397 called magic characters because of the fun and games you can have with them
403 \ - (backslash) the escape character itself
408 Without trying to explain it here, note that {:set nomagic^M} turns off the
409 special meanings of all but the ^ caret, $ dollar-sign, and backslash
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
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).
426 {:q!^M} does the trick. If you have not made any changes, the exclamation
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
433 to the Unix prompt. This will provide an unmodified copy of this file for you,
434 which might be necessary if you accidentally destroyed the copy you were
435 working with. Just do a search for the last section you were in: e.g.
440 Here are a few more commands that will move you around on the screen. Again,
443 {H} - puts the cursor at the top of the screen (the 'home' position)
445 {M} - puts the cursor in the middle of the screen
447 {L} - puts the cursor at the bottom of the screen.
449 Try typing {HML} and watch the cursor.
451 Try typing {5HM5L} and note that 5H puts you five lines from the top of the
452 screen, and 5L puts you five lines from the bottom of the screen.
456 Up to this point we have concentrated on positioning in the file, and
457 positioning on the screen. Now let's look at positioning in a line. Put the
458 cursor at the beginning of the following line and type {z^M}:
462 The test line should now be at the top of your screen. Type {w} several times.
463 Note that it moves you forward to the beginning of the next word. Now type
464 {b} (back to the beginning of the word) several times till you are at the
465 beginning of the line. (If you accidentally type too many {b}, type {w} until
466 you are on the beginning of the line again.) Type {wwwww} (five w's) and note
467 that the cursor is now on the colon in the sentence. The lower-case w command
470 type {0} (zero, not o 'oh'): this moves you to the beginning of the current
471 line. Now type {5w} and notice that this has the effect of repeating {w} five
472 times and that you are now back on the colon. Type {0} (zero) again. To
473 ignore the delimiters and to move to the beginning of the next word using only
475 delimit the words, use the {W} command: upper-case W. {B} takes you back a
478 Note that the commands {wbWB} do not stop at the beginning or end of a line:
479 they will continue to the next word on the next line in the direction specified
482 If you are interested in the END of the word, and not the BEGINNING, then use
483 the {e} and {E} commands. These commands only move forward and there are no
484 corresponding 'reverse search' commands for the end of a word.
486 Also, we have been using the {'} command to move the cursor to a position that
487 we have previously marked with the {m} command. However, position the cursor
488 in the middle of a line (any line, just pick one) and type {mk}, marking that
489 position with the letter k. Now type a few returns {^M} and type {'k}.
490 Observe that the cursor is now at the beginning of the line that you marked.
491 Now try {`k}: note that this is the reverse apostrophe, or back-quote, or grave
492 accent, or whatever you want to call it. Also note that it moves you to the
493 character that was marked, not just to the line that was marked.
495 In addition, the {``} command works just like the {''} command except that you
496 are taken to the exact character, not just to the line. (I'm still not
501 There are several commands to move around on the screen on a character by
504 l - moves the cursor one character to the RIGHT
505 k - moves the cursor UP one line
506 j - moves the cursor DOWN one line
507 h - moves the cursor one character to the LEFT
511 For this and following sections you will need to use the ESCAPE key on your
512 terminal. It is usually marked ESC. Since the escape key is the same as
513 typing {^[} we will use ^[ for the escape key.
515 Probably the most often used command in an editor is the insert command. Below
516 are two lines of text, the first correct, the second incorrect. Position your
517 cursor at the beginning of Line 1 and type {z^M}.
519 Line 1: This is an example of the insert command.
520 Line 2: This is an of the insert command.
522 To make line 2 look like line 1, we are going to insert the characters
523 'example ' before the word 'of'. So, now move the cursor so that it is
524 positioned on the 'o' of 'of'. (You can do this by typing {^M} to move
525 to the beginning of line 2, followed by {6w} or {wwwwww} to position the cursor
526 on the word 'of'.)
528 Now carefully type the following string and observe the effects:
529 {iexample ^[} (remember: ^[ is the escape key)}
530 The {i} begins the insert mode, and 'example ' is inserted into the line:
531 be sure to notice the blank in 'example '. The ^[ ends insertion mode,
532 and the line is updated to include the new string. Line 1 should look exactly
535 Move the cursor to the beginning of Line 3 below and type {z^M}:
537 Line 3: These lines are examples for the 'a' command.
538 Line 4: These line are examples for the '
540 We will change line four to look like line three by using the append command.
541 We need to append an 's' to the word 'line'. Position the cursor on the 'e'
542 of 'line'. You can do this in several ways, one way is the following:
543 First, type {/line /^M}. This puts us on the word 'line' in Line 4
544 (the blank in the search string is important!). Next, type {e}. The 'e' puts
545 us at the end of the word. Now, type {as^[ (^[ is the escape character)}.
546 The 'a' puts us in insert mode, AFTER the current character. We appended the
547 's', and the escape ^[ ended the insert mode.
550 inserting text BEFORE the cursor, and {a} begins inserting AFTER the cursor.
552 Now type {Aa' command.^[}. The cursor is moved to the end of the line and the
553 string following {A} is inserted into the text. Line 4 should now look like
556 Just as {A} moves you to the end of the line to begin inserting, {I} would
557 begin inserting at the FRONT of the line.
559 To begin the insertion of a line after the cursor line, type {o}. To insert a
560 line before the cursor line, type {O}. In other words {o123^[} is equivalent
561 to {A^M123^[}, and {O123^[} is equivalent to {I123^M^[}. The text after the
565 have to experiment to discover what your terminal does. Once in the insert
566 mode, if you make a mistake in the typing, ^H will delete the previous
567 character up to the beginning of the current insertion. ^W will delete the
568 previous word, and one of ^U, @, or ^X will delete the current line (up to the
569 beginning of the current insertion). You will need to experiment with ^U, @,
574 Position the cursor at the beginning of line 5 and {z^M}:
579 To make Line 6 like Line 5, we have to delete the 'n', the apostrophe, and the
580 't'. There are several ways to position ourselves at the 'n'. Choose
584 {^M7w6l} or {^M7w6 } (note the space)
585 {^M3fn} (finds the 3rd 'n' on the line)
587 Now {xxx} will delete the three characters, as will {3x}.
589 Note that {X} deletes the character just BEFORE the cursor, as opposed
590 to the character AT the cursor.
592 Position the cursor at line 7 and {z^M}:
597 To change line 8 into line 7 we need to change the 'c' in 'could' into a 'w'.
598 The 'r' (replace) command was designed for this. Typing {rc} is the same as
599 typing {xic^[} (i.e. delete the 'bad' character and insert the correct
600 new character). Therefore, assuming that you have positioned the cursor on the
601 'c' of 'could', the easiest way to change 'could' into 'would' is {rw}.
603 If you would like to now change the 'would' into 'should', use the substitute
605 (replace) replaces the current character with another character, while 's'
606 (substitute) substitutes the current character with a string, ended with an
611 substitutes the whole line.
613 Position your cursor at the beginning of line 9 and {z^M}.
618 To change line 10 into line 9, position the cursor at the beginning of 'most',
624 into one line. To do this, position the cursor on the first of the two lines
629 Finally, before we review, let's look at the undo command. Position
632 Line 11: The quick brown fox jumped over the lazy hound dog.
633 Line 12: the qwick black dog dumped over the laxy poune fox.
635 Type the following set of commands, and observe carefully the effect of each
636 of the commands:
642 looks like it did before you typed in the command strings. Now type:
646 and then type {u}: the cursor jumps back to the line containing the second
647 change you made and 'undoes' it. That is, {U} 'undoes' all the changes on the
648 line, and {u} 'undoes' only the last change. Type {u} several times and
651 Caveat: {U} only works as long as the cursor is still on the line. Move the
652 cursor off the line and {U} will have no effect, except to possibly beep at
653 you. However, {u} will undo the last change, no matter where it occurred.
657 At this point, you have all the commands you need in order to make use of vi.
659 well as introduce new commands that make the job of editing more efficient.
660 Here is a brief review of the basic commands we have covered. They are listed
661 in the order of increasing complexity and/or decreasing necessity (to say that
667 comfortable with it. The convenience of some of the more exotic commands may
668 then be apparent and worth the extra investment of time and effort
671 to get into the editor from Unix: {vi filename}
672 to exit the editor
676 save a file without exiting the editor {:w^M}
677 write the file into another file {:w filename^M}
679 before the cursor {i ...text... ^[}
680 at the beginning of the line {I ...text... ^[}
681 after the cursor (append) {a ...text... ^[}
682 at the end of the line {A ...text... ^[}
683 after the current line {o ...text... ^[}
684 before the current line {O ...text... ^[}
685 delete the character ...
686 under the cursor {x}
687 to the left of the cursor {X}
690 find a string in the file ...
693 repeat the last search command {n}
694 repeat the last search command in the
696 find the character c on this line ...
699 repeat the last 'find character' command {;}
704 undo all changes to the current line {U}
705 undo the last single change {u}
706 move forward in the file a "screenful" {^F}
707 move back in the file a "screenful" {^B}
708 move forward in the file one line {^M} or {+}
709 move backward in the file one line {-}
710 move to the beginning of the line {0}
711 move to the end of the line {$}
714 move forward to the end of the next word {e}
715 to the end of the word, ignoring punctuation{E}
718 return to the last line modified {''}
719 scroll a line onto the top of the screen {^Y}
720 scroll a line onto the bottom of the screen {^E}
721 move "up" in the file a half-screen {^U}
722 move "down" in the file a half-screen {^D}
723 move the cursor to the top screen line {H}
724 move the cursor to the bottom screen line {L}
725 move the cursor to the middle line {M}
728 move UP in the same column {k} or {^P}
729 move DOWN in the same column {j} or {^N}
730 mark the current position, name it x {mx}
731 move to the line marked/named x {'x}
732 move to the character position named x {`x}
733 move to the beginning of the file {1G}
734 move to the end of the file {G}
735 move to line 23 in the file {23G}
736 repaint the screen with the cursor line
737 at the top of the screen {z^M}
738 in the middle of the screen {z.}
739 at the bottom of the screen {z-}
741 More information on vi can be found in the file vi.advanced, which you can