This document is not meant to rewrite the man pages for vi or be comprehensive. This is meant to be a good overview of executing functions commonly found in any text editor. The vi editor is a mode-based editor. The important modes are: INPUT mode (which is used to enter text), VISUAL mode (used to navigate the document), and COMMAND mode (which is used to exe- cute commands such as save, delete and search-and-replace). The document opens in VISUAL mode. COMMAND mode will display a : cursor at the bottom of the screen. COMMAND mode is entered by typing the : key while in VISUAL mode. Only one mode is available at a time. Therefore, not all com- mands are available to all the time. To start vi with a new file, either execute vi and use the w com- mand with a filename in COMMAND mode, or type vi with a filename unique to the target directory. To edit an existing file just use vi with as the name of the file. A useful feature of vi is the vi -r feature. If a user is work- ing and their terminal crashes vi saves their work in a temporary file. This file can be resurrected by using vi -r when the user logs in again. The following list does not contain all of the commands, just the ones most used. To see all of the commands, read the vi man page (using man vi). When in doubt about what mode is active, hit the [ESC] key (sev- eral times) which will always go to the VISUAL mode. If odd things start to happen, make sure the [CAPS LOCK] key light is off on the keyboard. Most of the visual mode commands can be done multiple times with one command by putting a number in front of the command (for ex- ample, typing 3dd will delete 3 lines). Miscellaneous important commands: undo - only works for one com- mand. redo the last command redraws the screen. Useful if a wall message comes and messes up the display. joins two lines together. prints the filename and the line number where the cur- sor is located. Note: Unless the arrow keys (or numeric keypad keys) are mapped as described in the .exrc section of this docu- ment, NEVER use the arrow keys. It will insert control charac- ters in the document. Moves the cursor left. Moves the cursor up. Moves the cursor down. Moves the cursor right. go up a page. go down a page. go to the end of the document. go to the beginning of the document. go to line . go to the beginning of the line. go to the end of the line. go to the beginning of the next word. go to the beginning of the previous word. The following commands are used in the COMMAND mode: write (save) the file. write the file to . write the file and quit. overwrite file if it already exist. quit without saving. revert to saved. Typing ZZ while in VISUAL mode will save and quit the document. enter INSERT mode to the left of the cursor. enter INSERT mode at the beginning of the line. enter INSERT mode with a new line underneath the current line. enter INSERT mode with a new line above the current line. enter INSERT mode with the character at the right of the cursor. enter INSERT mode at the end of the current line. change line beginning at cursor. change word. change the case of the character under the cursor. transpose two letters. replace one character. replace characters until [ESC] is hit. delete line. delete word. Punctuation is considered a separate word. delete one character. delete line from cursor on. From the COMMAND line: read and insert the contents at the cursor. Using the delete and change commands described above and the yank commands described in this section put the delet- ed/changed/yanked text in an unnamed buffer which can then be re- trieved by using the put commands described in this section. Read the "Using Buffers" section in this document for more infor- mation on cutting or pasting. yank a line. Yank copies some- thing without deleting it. yank word. put contents of the buffer in the line after the cursor. put contents of the buffer in the line before the cursor. If the contents of the buffer is less than a line, for example, the contents after the dw (delete word) command is executed, then the put commands will put the word on the same line as the cur- sor. To find a word in a document, use the following commands. The command can be a regular expression following the Unix conventions. Execute "man regex" for a description of regu- lar expressions. search document down for next occurrence of . Without a , searches for last search string. search document up for next occurrence of . Without a , searches for last search string. find next in current line. One of the most useful features of vi is the abil- ity of running sed scripts on a document while in COMMAND mode. These can get complicated, but the most used function, a find and replace is fairly simple. The syntax, while in COMMAND mode, is: ,s///g If the , is left out, the changes are only done on the line that the cursor is on. If % is used instead of ,, the changes will be throughout the entire document. The last line of the document can be denoted by using the $. The and are regular expressions. To delete some- thing type nothing in the position. The last g tells the command to look at all occurrences of the on a line - the default is just the first occurrence. Examples: To find every occurrence of the words "data base" and change them to "database", use: %s/data base/database/g To find and delete the first occurrence of the word "one" between lines 2 and 5 use: 2,5s/one// To remove the ^M symbol appended to the end of the line by some text editors, use: %s/.$// To delete every .bp macro in a document, use: %s/^.bp// Warning: if ic (case-insensitive) is set (see the explanation in the .exrc section) then the sed changes will also be case insen- sitive. This may not be a good idea because it will change the capitalization of words in titles and error messages which should not be changed. Always be very careful of a global change, it may have unexpect- ed and devastating results to a document. To undo a global change, the u will work globally, but only if no other change has been made since the global change. It's always a good idea to save the document or save the document under a different name be- fore doing a global change in the event the change has to be nul- lified. The most annoying thing about vi is the lack of a multi- ple undo function. However, vi does save the last 9 items deleted which can span multiple lines. Those are save in the 1-9 buffers. To access these deleted lines, type: "p which will paste the stuff in that buffer. "1p is the last thing that was deleted, "2p is the second to the last thing deleted, etc. The P command can also be used to paste the buffer above the cursor. It is possible to save text in user-defined buffers from a-z. This is useful in cutting and pasting text by storing a yanked line for use later. If a yanked line is going to be used soon, the easiest way is to do a yy and then a p to copy and paste. But, if a user YANKS something and then accidentally types i or o to insert the yanked line instead of p, the buffer saved by the yy is lost. Or if while looking for the spot to paste the line some other thing is adjusted the yy buffer will also be lost. The way to get around that is to save the line in a user-defined buffer. To save text in a user-defined buffer, precede a YANK, CHANGE or DELETE command with " and the name of the buffer it should be saved under. For example, to save a line that is being changed under the letter a, type: "aC[ESC] To get the original line back, the easiest way is with an undo (u), but to get the original line back after several commands have been executed, type: "ap To append to a named buffer instead of overwriting it, use the capital letter. WARNING: The numbered and named buffers are not saved when the file is closed. However, when working on multiple files at once (using the multiple windows available through vi) the buffers are available to all files. It is NOT RECOMMENDED to use this fea- ture of vi. following is an example of a .exrc file. If this file is in the $HOME directory or the working directory of the user, it will be read by vi when it is executed. It is useful for customizing keys and accessing vi options. The example remaps the arrow keys and the numeric keypad to move around the screen. The vi program is actually based on the ex program (hence, .exrc) and so the options are documented in the ex man page. Here is an example of a .exrc file which remaps the arrow keys and the numeric keypad to move around the screen: --------------------------------------------------------------------------------------- set showmode wrapmargin=5 report=1 ic set beautify nomesg wi=72 map ^W :set wrapmargin=8 map ^X {!}sort -b map ^[h 1G map ^[H 1G map ^[F G map ^[V ^B map ^[U ^F map ^[T ^Yk map ^[S ^Ej map ^[Q i map ^[P x map ^[L O map ^[M dd map ^[K D map ^[J DjdG$ map! ^[A ^[ka map! ^[D ^[ha map! ^[C ^[la map! ^[B ^[ja map! ^[L ^M map! ^[Q ^[ map! ^[R ^[ map ^[OA k map ^[OB j map ^[OD h map ^[OC l map ^[Ox k map ^[Or j map ^[Ot h map ^[Ov l map ^[Os ^F map ^[Oy ^B map ^[Ow 1G map ^[Oq G map ^[Op i map ^[On x map ^[OM ^M map ^[Oj :w^M map ^[Ou ^G map ^[Ok w --------------------------------------------------------------------------------------- The set command sets options in vi, some of the things set in the example below are: Display the current editor mode (such as INPUT MODE, REPLACE 1 CHAR, REPLACE MODE) in the lower right-hand cor- ner of the screen during visual and open mode. Reversed by noshowmode (nosmd). The ex, vi, and view default is noshowmode. In visual mode only, if n is greater than zero, a newline is au- tomatically inserted in an input line at a word boundary, so that lines end at least n spaces from the right margin of the terminal screen. The default is wrapmargin=0. All uppercase characters in the text are mapped to lowercase in regular expression match- ing. Also, all uppercase characters in regular expressions are mapped to lowercase, except in character class specifications. Reversed by noignorecase (noic). For explanations on all of the possible options, execute: man ex. Note, if nowrap is set, then the user will have to type in re- turns manually at the end of every line. Otherwise @ symbols will be displayed for long lines and will make the document dif- ficult to read online. To get a list of possible set commands while in COMMAND mode type "set all". To see [tab]s as control characters (^I) instead of white space, type "set list" while in COMMAND mode. To turn off the control characters type "set nolist" while in COMMAND mode. The map command is used to map keys to vi commands. The ^ symbols are for control characters. For example, the ^[OA is the [up-arrow] on the numeric keypad. The way to get that escape sequence is to hit [CTRL]-v and then [CTRL]-. Keep in mind the vi COMMANDS are mapped, not the letters. So, to map the [CTRL]-A key to print out "Updates to RDBMS", type in: map [CTRL]-A iUpdates for RDBMS[CTRL]-[ESC] The map! instructs vi not to map a key. In the COMMAND mode it is possible to map a key for an individual file, but the mapping will be lost as soon as the file is closed. The "map ^X {!}sort -b" uses the ! functionality of the vi command from COMMAND mode. Any unix command can be run from Com- mand mode. For example, to see what the working directory is be- fore saving a file, type in: ! pwd from the COMMAND mode.