vi TUTORIAL - Scott Jennings -------------------------------------------------------------- START VI AND EDIT FILE "FILENAME" vi filename INSERT TYPING (TEXT) IN FRONT OF CURRENT LOCATION POINTER (CURSOR) i INSERT TEXT AT BEGINNING OF CURRENT LINE I INSERT TEXT AFTER CURSOR (APPEND) a INSERT TEXT AT END OF CURRENT LINE A DELETE CHARACTER AT THE CURSOR x DELETE TO END OF LINE FROM CURSOR d$ DELETE WHOLE LINE dd MOVE LEFT h MOVE DOWN j MOVE UP k MOVE RIGHT l MOVE ONE PAGE DOWN
d MOVE ONE PAGE UP u GO TO END OF LINE $ GO TO BEGINNING OF LINE 0 GO TO LINE # N nG FIND "SOMETHING" FROM CURRENT LOCATION FORWARD / something (and ) FIND "SOMETHING" FROM CURRENT LOCATION BACKWARD ? something (and ) REPEAT LAST FIND n UNDO LAST CHANGE u COPY CURRENT LINE yy CUT CURRENT LINE dd PASTE COPIED/CUT LINE p MARK BEGINNING OF BLOCK "a" ma COPY BLOCK FROM MARKER "a" TO CURSOR "ay'a PASTE BLOCK "a" "ap QUIT WITHOUT SAVING :q! SAVE AND QUIT :wq SAVE :w SAVE AS "NAME" :w name To enter the programs that you design for this class into the computer, you will use a program called a text editor, specifically vi. To use vi, you will need to learn a small set of commands. These are fairly simple, however, initially you may want to use a command reference of some sort. There are two modes (ways that vi works) that you need to know about. First is Command mode. In this mode, you can move around the document, cut, paste, delete, find, save, and exit. The vi program starts in Command mode. The second mode is Insert mode. This is when you will enter your text. No commands will work when you are in Insert mode. You can enter Insert mode with the following commands: Description Key to press To insert before current location i To insert at beginning of line I To insert after current location (append) a To insert at end of line (append) A To exit Insert mode and return to Command mode, press the key. Now let's go through the process of entering a program step-by-step. Say you want to call your program HW1. So, once you have logged in to the computer, type: vi Test (and press the key) Now the vi program will start up. If Test is a new file, the screen will be filled with lines that begin with a '~' character. These are just empty lines, so don't worry about them. Next, to begin typing in your program: press i and then start typing (this is Insert mode) Don't try to move around while in Insert mode. If you need to move around, press the key to go to Command mode, then you can move. The following is a list of some of the movement commands: Description Key to press move left h move down j move up k move right l go up a page u go down a page d go to beginning of line 0 go to end of line $ move to line number n nG To delete something, you must be in Command mode (press ). Then go to the item that you want to delete using the movement keys. Now you have a choice of the following options: Description Key to press delete character at the cursor x delete to end of line from cursor d$ delete whole line dd When these items are deleted, they are copied into the undo buffer. So you can use either undo or paste to retrieve the deletion. >From Command mode: Description Key to press undo last action u paste contents of undo buffer p Note: the paste command will paste the item to the line under the cursor The following command allows you to search for a specific word or character within the document that you are editing. >From Command mode: Description Key to press find next "item" / item (no space) (and press ) find previous "item" ? item (no space) (and press ) repeat last find n Now for the stuff that saves a lot of time: Cut, Copy, and Paste. To cut an item, all that you have to do is delete that item. For example, if you just used dd to delete a line, that line has been cut. Now you can move around and find the place that you want to put it. Then you will use p to paste it back (see note about paste). To copy an item involves a few more steps. One way is to use the 'yank' method. From Command mode: Description Key to press copy (yank) current line yy copy from cursor to end of line y$ Once you have yanked something, you can move to where you want and use 'p' to paste the item that you copied. Another way to copy is using the 'block' method. To do this, you must do the following in Command mode: 1) move to the beginning of the area that you want to copy 2) press the m key and then the a key. This creates a block marker named a 3) move to the end of the area that you want to copy 4) press " then a then y then ' then a ( so "ay'a ). This will copy the text from marker a 5) move to where you want to paste the block 6) press " then a then p ( so "ap ) to paste Saving and Quiting It is very important to save your work often (about every fifteen minutes). When you are new at vi, it is easy to make a mistake. If you did not save your file, you could lose hours of work. So here is how. From Command mode: Description Key to press save current file :w save and quit vi :wq save as "name" :w name quit without saving :q! Well, that the basics of the vi program. There is a lot more to vi than presented here. Many sources are available in UNIX books or on the Internet (search for UNIX and vi). One last important thing, if something weird is happening, just press the key a couple times (not twenty!) and then just wait a few seconds and then start again from Command mode.