A Beginner's Guide to Using the VI Editor by Jim Leonard, 8/2/1996 Last edited 11/26/2003 History (slightly inaccurate for brevity) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Back in the old days (like, the mid-seventies), terminals only had a standard keyboard, and an key. No mouse, no graphics; all commands had to be entered without the use of a control key, alt key, or any function keys. The solution? Use the normal keys to enter commands or move around, and then enter a special editing command to actually start entering text. You could then leave this editing command by hitting the key. Why Would I Want To Use VI? ^^^^^^^^^^^^^^^^^^^^^^^^^^^ VI sounds odd, but it worked great for the time. "But why is it still being used in today's 101-key keyboard world when there are editors that use all of my keys?" you might ask... Well, for two reasons: - It comes standard on every Unix platform, and it *always works* - Once you learn it, it is faster and more flexible to use than almost any other editor. One reason for this is the designers of ex (VI's father) and VI worked with extremely slow computers, and the less you had to tell the computer, the faster you got your information back. Most common semi-complicated operations in VI are less than 4 keystrokes. There are also a couple of other reasons you might want to learn VI: - Most VI implementations work with the file in-place on the disk. As such, you can work with any size file. This also provides some automatic save/fail-safe capabilities with some VI clones, like Elvis or VIM. - Because VI works with the file in-place, it doesn't require a lot of memory. (Other editors load the entire file into memory.) If you're on a local Unix box, this will conserve precious RAM for your other processes. But why *wouldn't* you want to learn VI? - VI, because of its age and odd nature, has an (understandably) high learning curve. If you don't learn new software easily, you probably shouldn't tackle VI. - Because VI works with files in-place on the disk, editing can be slow if you have a large file and a slow hard disk. :-) And don't use VI on an old laptop--the constant hard disk access will quickly deplete your battery. ;-) So You Want To Learn VI: ^^^^^^^^^^^^^^^^^^^^^^^^ VI is a "modal" editor, which means it has two modes: Command mode, and Editing mode. Both modes are controlled using the same keyboard; you enter editing mode (entering text normally) using an "editing mode key", and then use the key as a "escape" to get from editing mode back to command mode. Usually, you do a command-mode sequence, then an editing-mode sequence; for instance, moving to where you want to add text, then actually adding it. So, let's start with the most common command-mode keys: Moving around. You can move up, down, left, and right with the h-j-k-l combo: k h l j k=up, l=right, j=down, h=left. While you're in command (movement) mode, you can do stuff other than edit. You can delete individual characters with "x", and you can delete whole lines with "dd". I know that "x" might make sense, and "dd" might not, but it does make sense--you'll have to trust me. :-) Enough With Commands--I Want To Enter Text! ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ You actually start inserting text with the "i" command. After you hit "i", you will start entering text with everything you type, like a normal keyboard. To get out of editing mode, hit . (Then you can start using commands again, like moving around.) Simple Enough, But I Want To Save My Work ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ When you're done entering text, you hit to get into command mode if you're not already there, and then you can type a "meta-command" to save your work or leave the editor. For instance: - ":w" will save the file ("write") - ":q" will quit the editor ("quit") - ":wq" will write the file, then quit - ":q!" will quit the editor even if you have changes waiting to be saved Where Do I Go From Here? ^^^^^^^^^^^^^^^^^^^^^^^^ This is only the tip of the iceberg--there's a whole load of cool stuff you can do with VI *fast*. For instance, in command mode, any command prefaced by a number will be executed that number of times. Here's a quick example: 8k - Move up 8 lines 40x - delete 40 times in a row d3j - delete the current line, plus the next three lines Typing "man vi" at the command prompt will list an entire set of commands and functions that you can do in VI. Hopefully, this document was enough to get you started. If you need more information, consult the man page for vi or the O'Reilly book "Learning the vi editor".