Mike guymal.com
My mic to the world...


Home Back to Tech Corner

vi Basics


The lost art of VI programming



If you're not already familiar with "vi", you'll find this short introduction very helpful. Even if you are familiar with "vi", we hope you'll find this to be a good review.

The most important thing to know about "vi" is that it is always operating in one of two modes: Insert mode or Command mode. In insert-mode, everything you type will be inserted into the file you are editing, up to the point when you exit insert mode by typing Esc. In command mode, you can cut and paste, move the cursor, search, delete lines, delete characters, modify words, etc.

Secondly, you need a warning. Almost every key on the keyboard (lower-case, upper-case and control-shifted) does something in command mode. So, be careful what you type. It's unlikely you'll ever discover anything useful about "vi" by accident.

Since everyone makes typing mistakes (some having disastrous consequences in command mode), the most useful commands are the "undo" commands: u and U (capital-U). Lower-case "u" will undo the immediately preceding change, no matter how complex or extensive. "u" is definitely one of the most useful "vi" commands.

As long as your cursor has not left a line of text, upper-case "U" will undo all changes which have been applied to the current line of text, restoring the line to its original state when the cursor entered the line previously.

Lastly, you need to master a set of basic commands. When "vi" starts up you'll find it in command mode, so your first commands will normally be to move the cursor and start inserting.

Enter text:

i...Esc start inserting at the cursor position. All characters typed after "i" and before [Esc] are inserted.
a...Esc start appending text after the cursor position. All characters typed after "a" and before [Esc] are inserted.

Move cursor:

h,j,k,l move cursor left, down, up, and right.
$ move cursor to the end of the current line.
:1 go to line 1.
:$ go to last line.

Other:

dd delete the line the cursor is on.
x delete the character the cursor is on.
p, P paste the last deleted text at or after the cursor position.
u undo the previous command.
U undo changes to the current line.
J join following line to end of current line.
control-L re-paint your screen. This is sometimes useful when using "vi" over phone lines.
:q! abandons ALL changes and aborts "vi".
ZZ save all changes and exits "vi".
? help

While inserting, you'll need to type a carriage-return at the end of each line. Otherwise, your text will be one very, very, very long line. "vi" does support automatic word wrapping. If you need this or any other feature not mentioned above, please contact FullFeed for assistance.

Well, that's a basic introduction to the "vi" editor. If you'd like to learn more about "vi", refer to any introductory Unix text. Many introductory books about the Internet contain sections about how to use "vi", also. Unfortunately, on-line "vi" help through "man vi" only tells you about various options for starting up the program, not how to actually use it once you're in it.


2001 Guy Malachi, All Rights Reserved