NOTICE: The article below is copyrighted. You can distribute it freely, but you must distribute it as is, with no changes, and with the copyright notice at the bottom. ----------------------------------------------------------------------- VI - THE WORLDLY EDITOR 1. Intro Whether you are an Internet surfer, a USENET aficionado, a UNIX(tm) programmer or a plain user in a mixed platform environment, the most common utility you are likely to encounter is the vi editor. It's everywhere, on every UNIX system, and it is employed for anything from code editing to typing an occasional email letter. To a traditional DOS or Mac user, vi seems foreign at first mainly because it sees text input command as any other editing instruction and does not allow stacking it with other commands which almost all DOS and Mac editors do allow. For example, when inserting text in DOS "EDIT", can you press PageDown to go down? Yes, naturally, you will say. Not in vi, however, since text inserting does not have any special ability to be mixed with other commands. In vi you would first end text inserting command by pressing "Esc" (that's how all text input commands are terminated) and then you would press Ctrl-F (for "Forward Page"). While it may seem counter-intuitive to DOS and Mac users in the beginning, it allows the use of single letter keystrokes as editing instructions, making vi not only a very fast editor, but also a very frugal one when used over the wire. This particular design model has also allowed the author to make commands easy to remember and easy to deduce once some of them are memorized. This easy inference is necessary in vi since its "bandwidth frugality" precluded it from having menus. When vi is first started, it expects commands, be it text input, deletions, changes, searches. Following the command, an argument usually needs to be given, for example, following command "i" (insert text at the cursor), one would type text and then end the command with "Esc". For deletion, one would press "d" and then an argument, for example "w" for "word", or "3w" for "three words" or "}" which in vi-speak means "to the end of the paragraph". If simple mnemonic commands are not sufficient, command line is provided through pressing ":". From there long commands can be executed to be applied to ranges of lines in text. Even external programs can be used as filters to process parts of the text. If that is still not sufficient, a multi-line command facilities can be employed after pressing "Q". 2. Commands Since the best way to learn is through exercise, below you will find a cheat-sheet with vi commands. If you do not have an access to the UNIX system yet, you can already start practicing by downloading a DOS version of vi (elvis) from one of the many anonymous ftp sites. vi cheat-sheet a. cursor movements (items below are sometimes called "objects"): h - left one character l - right one character j - down one line k - up one line w - right one word b - back one word $ - to the end of line 0 - to the beginning of the line ) - right one sentence ( - left one sentence } - right one paragraph { - left one paragraph Ctrl-F - forward one page Ctrl-B - back one page G - go to (without arguments, go to end of file) b. deleting: d - delete then add one of the cursor movement symbols to show what should be deleted, i.e.: d$ - delete to end of line d0 - delete to the beginning of the line d} - delete to the end of paragraph dd - delete delete (delete the whole line) x - delete character cursor is on c. other basic commands: r - replace one character ZZ - save and exit (hold down shift and press "z" twice) y - yank (copy into temporary buffer) then add cursor movement symbol to show what should be copied, for example: y) - copy to the end of sentence Y - yank line cursor is on p - paste below cursor line (deleted or copied text) P - paste above cursor line u - undo last editing command /sometext - search for "sometext" d. any command can take numeric argument before the name of "object", i.e.: 5dd - delete 5 lines beginning with cursor line (or) d5d - same 2dw - delete two words (or) d2w - delete two words c3w - change 3 words 3Ctrl-B - move up three pages 1G - go to the first line e. external commands can be performed on the selected text (in lines) if command is started with "!", i.e.: !}fmt - reformat paragraph to 72 columns f. command line (sometimes called "ex mode"): : g. in ex mode "set" command can be executed to customize editing environment, i.e.: :set all - will show the state of all options :set number - will show on the screen numbers of all lines :set autoindent // obvious h. in ex mode any ex command can be performed on the range of lines, i.e.: :18,24 del - delete from line 18 to line 24 :23,48 copy 17 - block from line 23 to 48 copy to line 17 :2,17 move 92 - block from line 2 to 17 move to line 92 i. in ex mode any external UNIX command can be performed on the range of lines if line range is superseded by "!": :11,16! sed -e "s/^/\/\*/" -e "s/$/\*\//" (the command above wraps the block of text with "C" style comments - /* text */. It can be done easier, but this is an example) :14,19! sort -r +3 (sort the table in reverse order by fourth column) j. file commands in ex mode: :r somefile - read in "somefile" :x - save and exit (if file is "Read Only", this command will exit without saving) :wq - write and quit (same as above) :w - write (save) :w somefile - save this file as "somefile" :q - quit without saving :q! - quit without saving if changes were made k. text input commands (all require "Esc" to terminate): i - insert text before the character cursor is on I - insert text at the beginning of the line a - append (insert text after the character cursor is on) A - append text to the end of the line c - change (replace previous text with new one) takes arguments just like the delete command - it is a fast and powerful way of changing original text - much more so than typical "overwrite" R - start overwriting text o - start entering text at the beginning of the new line below the cursor O - start entering text at the beginning of the new line above the cursor l. if in doubt, press "Esc" 3. Startup File Having explored vi commands somewhat, we are now ready to create a default editing environment through the use of .exrc (or elvis.rc if you use elvis DOS vi clone), the configuration file which resides in user's home directory (for DOS users simply set environment variable "home" to a directory where you wish to keep elvis.rc file). .exrc typically contains at least three types of entries: set commands to create preferred editing environment, map commands to assign functions or macros to keys, and ab commands to either map abbreviations or automatically correct common spelling errors. When mapping escape sequences of some keys, we may need to "quote" them, that is, to insert them literally. This can be accomplished in vi by issuing insert text command and pressing Ctrl-V before the key we wish to quote is pressed. Now let's look at a sample .exrc: set autoindent set wrapmargin=8 set ignorecase map #0 :w^M map #4 !}fmt^M ab teh the ab cutsomer customer Lines 1 and 2 set autoindent mode on and turn on wordwrap at column 72. Line 3 specifies that all searches are to be case insensitive. Lines 4 maps function key 10 to "save" command. Line 5 maps function key 4 to "reformat from the cursor position to the end of the paragraph". Finally, lines 6 and 7 map some common misspellings to the correct spelling. Note that ^M on lines 4 and 5 are created by pressing Ctrl-V and then hitting Ctrl-M. With a little creativity you can make vi look as personal as you wish. NOTE: After creating .exrc make sure that EXINIT environment variable is not set (type set at the command prompt) - it would overwrite whatever settings you entered into .exrc. Also make sure that .exrc contains no blank lines - they are not allowed. 4. Editing Multiple Files Editing multiple files in vi is possible in two different ways: - invoke vi with multiple files on the command line - use its ex command :e from within vi Once the files are loaded, movement from one file to another can be accomplished using following commands: - ex command :n for "next file" - ex command :e# for "previous file" - built-in command ^^ (Ctrl-^) for "last edited file, keep cursor on last edited line" 5. Using Named Buffers It is possible to copy or delete text from any file to a named buffer, and to keep as many as 26 of those buffers around for convenience. Buffers are created using lower case single letters (using upper case letter means "append to the existing buffer". The command beginning buffer operation is " (double quotation mark). Following it is the name of the buffer and then description of the operation to be performed: "ay} - yank (copy) from cursor to end of paragraph into buffer "a". If buffer exists, overwrite it. "A12d - delete 12 lines from cursor down and append it to the existing buffer "a". "ap - take contents of buffer "a" and paste it below cursor line. Notice that lower case name for a buffer causes it to be created every time a command is issued. A capital letter will append the new text to to the existing buffer. 6. Macros Buffers can also act as macros if they contain valid vi or ex commands. To execute such a macro, type: @a - where "a" is the name of the buffer Example: From a file containing definitions of complex macros, yank one paragraph (say, 20 lines) to a named buffer "a". Then return to a file you were originally editing and type @a. The commands contained in the buffer will be executed on the edited file. It is possible to make files act as vi macros. To do that, give an ex command: :so filename The most powerful macros, however, are created using programs combined into shell scripts (or DOS batch files), and acting as filters on the text sent to them from within vi. Such shell scripts or programs must take standard input and send results to standard output (which most UNIX tools do). With a little practice, using vi becomes a second nature. Thousands of happy users around the world attest to that. Copyright (C) 1994 Tony Porczyk - UNIX is a trademark of X/Open