vi tutorial #1: The Basics This lesson lasts 10-15 minutes and teaches simple editing. Lines which begin with >>> mark exercises you should try. When you want to exit this tutorial type ZZ (type capital Z, twice). When you type commands in vi they do not appear on the screen. If the letters you type unexpectedly appear on the screen, press the ESCAPE key. BASIC CURSOR MOVEMENT --------------------- To move through the tutorial use ^D (control d) and ^U (control u). ^D Move DOWN one half-screen (depress the control key and type d) ^U Move UP one half-screen (depress the control key and type u) >>> Now type ^D (control d) and ^U (control u) to move down and back up. When you are done reading a screen, you are expected to type ^D to move down to the next screen. You must remember to type ^D throughout the tutorial. To move the cursor line by line, or character by character, use the four keys h, j, k, and l. h Move left one character j Move down one line k Move up one line l Move right one character You will notice that these keys are in a straight line on the keyboard. Study the diagram below showing the function of h, j, k, l. UP ....... ....... ....... ....... : : : : : : : : LEFT : h : : j : : k : : l : RIGHT :.....: :.....: :.....: :.....: DOWN >>> Now type j or k a few times to bring the cursor to this line. >>> Try moving off the right end of a line using l . Notice that >>> vi will not allow you to move off the end of the line using l . >>> Likewise, you cannot use h and l on a blank line. >>> Try moving past the bottom of the screen using j . Notice how >>> how the screen scrolls downward. >>> Now practice using k to move up, and h to move left. DELETION -------- To delete characters and lines, use x and dd. x X-OUT one character dd DELETE one line To undo your changes, use u. u UNDO last change only >>> Delete this SCRAP line. Move to this line with j or k , now type dd . >>> Try undoing the deletion with u . >>> Move to this line and x-out the Y's with x : "whY ask whY?" >>> Try undoing the deletion with u . Try typing u several times. >>> Notice that u only undoes the last change. Here are more lines on which to practice deleting and undoing (use: dd x u ) Emacs is an evil creation. Emacs is an evil creation. Emacs is an evil creation. Emacs is an evil creation. Emacs is an evil creation. Emacs is an evil creation. QUIT COMMANDS ------------- (DO NOT QUIT the tutorial at this time.) To quit a file without saving any changes you have made (for instance, with the dd or x commands) use :q! . To quit and save your changes, use ZZ . When you are editing your own files, you normally use ZZ to quit. :q! QUIT without saving changes (type a colon, then the letter q, then an exclamation point, and press RETURN) ZZ Exit and save any changes (type capital Z, twice) INSERTION --------- You enter insert mode with i or o . Anything you type during insert mode appears on the screen. When you are done inserting, press ESC to exit insert mode. Type ^[ (control [ ), if you do not have an ESC key. o OPEN a line for inserting text i INSERT starting at the cursor ESC ESCAPE from insert mode During insert mode, use your erase character (usually backspace or delete) to delete mistakes. The characters you delete will remain on the screen until you press ESC. >>> Insert your name and phone number below the next blank line. To do this: >>> Open a line below using o . >>> Type your first and last name. Press RETURN. >>> Then type your phone number and press ESC. >>> Use x to erase part of your phone number. >>> Type the date below your phone number. To do this: >>> Open another line using o . >>> Type the date and press ESC. >>> Type u to undo the insertion. >>> Insert a nickname between your first and last names, using i. To do this: >>> Move the cursor to the spot between your names using h, j, k, l. >>> Press i . >>> Type the nickname, use DELETE or BACKSPACE to erase any typos. >>> Then press ESC. On some computers, a line may be longer than the width of the screen. This means that a very long line may appear to be two lines on the screen. This happens when you keep typing without pressing RETURN at the edge of the screen. To avoid any confusion when you're inserting text, be sure to press RETURN before reaching the right edge of the screen. FILE BASICS ----------- To edit a file using vi, at the UNIX command line, type: vi filename where "filename" is the name of the file you want to edit. When you use vi to edit a file that does not exist, the file is not immediately created. The file is created the first time you save it, until then it only exists in temporary space. If you make a mistake typing the name of a file, just use :q! to abandon the new file. SUMMARY ------- These are the vi commands you should know after tutorial #1: ^D Move DOWN one half-screen (depress the control key and type d) ^U Move UP one half-screen (depress the control key and type u) h Move left one character j Move down one line k Move up one line l Move right one character dd DELETE one line x X-OUT one character u UNDO last change :q! QUIT without saving changes (type a colon, then the letter q, then an exclamation point, and press RETURN) ZZ Exit and save any changes (type capital Z, twice) o OPEN a line for inserting text i INSERT starting at the cursor ESC ESCAPE from insert mode and to use vi from a UNIX command line: vi filename You are now prepared to do simple editing on your own files. Practice using vi for a few days. Then take the second vi tutorial to learn more powerful and useful vi commands. Copyright (c) 1992 Jill Kliger and Wesley Craig. All Rights Reserved.