Vi is probably the most powerful text editor for *NIX, but if you have ever tried to use it, you probably walked away frustrated. This article walks through all the capabilities and features of Vi - from the basics, such as saving and quitting, to the more advanced topics of searching for strings.
Starting a vi session is ridiculously simple; type
$ vi
at your UNIX shell prompt, followed by a carriage return, and
vi will pop open an empty file for you to begin work. Alternatively, you could specify the file to open on the command line itself, like this:
$ vi myfile
Don't worry about the ~ signs in the editor - they're simply
visual indicators of the bottom of your file, and don't actually appear in your document.
The first - and most confusing thing - about vi is the various different modes the editor can operate in. For our purposes, I'll split them into two broad categories: "insert mode", and "command mode". "Insert mode" is where you'll be spending most of your time - it allows you to add, or insert, text into your document. "Command mode," on the other hand, lets you move around the document, delete or replace blocks of text, and access useful vi functions like buffers and ranges.
When vi first starts up, it's usually in command mode. To enter insert mode, type
i
vi will display a visual notification of the mode change in
the bottom left corner. You can now enter text into the document in the normal manner; when you're done, hit
<ESC>
to return to command mode.
To begin inserting text on
a new line, try
o
to enter insert mode with the cursor already positioned on
the next line, or
O
to enter it with the cursor on the previous line.
In
addition to inserting text, vi also allows you to overwrite existing text - this is known as "replace mode", and can be entered by typing
R
from command mode. Everything you type will now "write over"
existing text. If your intention was to replace a single character - for example, correcting a spelling error - use
r
to replace only a single character; the editor will
automatically return you to command mode after the character has been replaced.
Obviously, at some point, you're going to need to save your work. In vi, this is accomplished via the "write" command, abbreviated to
:w
To quit, use the handy "quit" command, known to family and
friends as
:q
vi also lets you combine frequently-used commands together -
to really impress the pretty girls, not to mention add a few points to your geek quotient, try
:wq
for powerful save-and-exit functionality. Alternatively, if
you've decided to bury whatever you've just written, try
:q!
which exits vi without saving any changes to the file being
edited.
Obviously, all these "colon" commands need to be executed in command mode - attempting them while in insert mode will result in them appearing as part of your document, and no professor likes to receive assignments interspersed with :wqs and :q!s. Well, actually, some of them do - but that's mostly the younger crowd, and what can you expect from them? Those punks actually think rock 'n' roll is dead...
This article copyright Melonfire 2000. All rights reserved.