In his last class, he taught you the basics of vi, the powerful *NIX text editor. Now Elias Flootburger returns in this hilarious sequel to theoriginal "Vi 101" tutorial. This time, the good professor has his handsfull with abbreviations, key mappings, autocommands and vi's powerfulvisual mode...not to mention his own out-of-control ego!Note: Most of the material in this article covers vim, the enhanced version of vi that is preinstalled on most modern *NIXes.
The last time we met, I told you that vi operates in either one of two modes - "command mode" and "insert mode".
Well, I lied.
Don't look so horrified - I hate to burst your bubble, but people lie all the time. And just so you know - Santa Claus doesn't exist, and the tooth fairy decided to get out of the business a few years ago; she's now in Las Vegas earning millions as a showgirl...
Anyway, getting back to vi - the editor also comes with a very powerful "visual mode", which allows you to select blocks of text and perform actions - cutting, copying, deleting - on them, in a manner similar to Windows-based editors like Microsoft Word.
Entering visual mode is simplicity itself - simply hit
v
while in command mode. Vi will display a visual notification
of the mode change at the bottom left corner of your screen. Now try using the various motion keys to move around - you'll notice that instead of moving you around the document, those keys now control a selection block which can be used to highlight sections of the text.
In addition to visual mode, there's also a "visual line" mode, which allows you to perform line-by-line selection and is activated by hitting
V [that's Shift-V]
and a "visual block" mode, which allows you to select
vertical, rather than horizontal, blocks of text - hit
^-V [that's Ctrl-V]
to activate this mode. In all these different visual modes,
the usual motion keys can be used to control the size of the selection block.
Now that you've got your text selected, it's time to do something with it - and vi comes with some built-in actions that allow you to interact with your selection. For example, type
d
to delete your entire selection, or
y
to copy it. You can then paste it by moving to the
appropriate point in your document and hitting
p
The "change" command, activated in visual mode by typing
c
will delete the selected block and enter insert mode at the
first character position of the deleted block.
If you're a programmer,
<
and
>
can be used to indent blocks of code left and right,
respectively. And
~
can be used to switch the case of the selected text, from
upper-case to lower-case and vice-versa.
A couple of other interesting commands in visual mode - type
o
to move the cursor between the upper left corner and lower
right corner of your selection block, or
gv
to repeat the previous selection.
To exit visual mode,
simply tap the
<ESC>
key, and you'll be returned to command mode.
As you'll
see after you play with it for a while, visual mode is extremely powerful. It comes in particularly handy when you're dealing with large text files, and need to transpose sections of text from one place to another - instead of counting the number of lines to be copied, and then doing a
10dd
or a
21yy
you can simply select the lines of text with the motion keys,
and use the operators above to transfer them to their new home. With power like this, who needs Santa Claus?
This article copyright Melonfire 2000. All rights reserved.