Tips on using vi ________________ Dick Willingale 1992-Dec-9 Forget HOW you do things in EDT or similar editors and just remember WHAT you can do. Forget the arrow keys and the keypad to the right of the main keyboard. Remember vi (and most of UNIX) is case sensitive. Avoid use of the caps. lock. Find out what the key is. On the SUN it is [ not the ESC button found on Pericoms etc. Unlike EDT in character mode the "normal" state of vi is in a "command" mode. In this mode you move around the file, search for strings, yank and paste and all that. Various characters get you out of this command mode into "input" mode in which what you type goes into the file. In this mode there is ONLY 1 escape, [. There are no special keys as in EDT. As I said, forget the arrow keys and the keypad, they all put more characters into your file. If you're not sure which mode vi is in use [ to force it into command mode. I found my most common mistake was to forget to terminate input mode with [. It's just a habit you must get into. vi was designed for people who type using the correct position for the hands. All the command keys are easy to reach. However almost all the keys do something and the shift key is important. Remember, case is important. Keep the 4 fingers of the right hand over j, k, l, ;, the traditional position. The keys h,j,k,l are your arrow keys, left, down, up and right so these are first finger, first finger, second finger, third finger. Watch the case. J joins the next line to the current line something rather different from j to move down a line. Searching for strings is very easy. Hit / (fourth finger right hand) followed by the text and . This is a good example of economy in vi. Very few keystrokes are required and the hands don't have to move much. No rushing around with keypads and gold etc.. There are a number of ways to get out of vi. The easiest exit is ZZ. Once again the case is important. Remember UNIX doesn't use version numbers so be careful when creating new versions of files. The following list are the commands which I have found most useful and cover most of what EDT does. When you start things will be a mess but soon you'll be wondering why you thought EDT was so good, honestly! F scroll forward 1 screen B scroll backwards 1 screen D scroll forward half screen U scroll backwards half screen #F scroll forward # screens #B scroll backwards # screens L refresh scrambled screen h move left 1 character j move to next line, same column k move to previous line, same column l move right 1 character $ move to end of line 0 (zero) move to start of line H move to top of screen L move to bottom of screen M move to middle of screen W move forward 1 word (delimited by space) B move backwards 1 word (delimited by space) E move to last character of current word G move to end of file #G move to line number # #x move x # times, e.g. 3W is move forward 3 words /string move forward to string, e.g. /SUBR is find SUBR ?string move backwards to string n move to next occurrence of string (previously set) N move backwards to string (previously set) D move backwards over autoindent x delete character at cursor X delete character before cursor dd delete current line d delete from current position to new position i insert text before cursor a insert text after cursor I insert text at start of current line A insert text at end of current line o insert text after current line (newline started) O insert text before current line (newline started) R overwrite (replace) from cursor [ terminate input mode (insert or overwrite) (escape) H erase last character in input mode (rubout) yy yank current line into buffer y yank from current to new position into buffer p paste deleted or yanked text after cursor P paste deleted of yanked text before cursor rx replace a single character with character x J join next line to current line % find matching character for (,),{,},[ or ] u undo last command U undo all commands on current line ZZ save changes and exit :q! quit without saving :f show current file, line # and total # of lines :set nu display line numbers :set nonu don't display line numbers :set nomagic switch off . [ * as magic characters :set magic switch on . [ * as magic characters (default) :r filename include an external file after cursor :w filename write complete edit buffer to file :n,mw filename write lines n to m to file :n,ms/str1/str2/g replace str1 by str2 in lines n to m (1,$ for all lines) :n,ms/str1/str2/gc replace with consultation ( to skip, y to do) :!command execute shell command %vi -r filename recover edit after a crash