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.
Finally, if you're a programmer, vi comes with a number of features that you should find useful. Here are some tips that might help you the next time you sit down to code The Next Big Thing.
1. Let vi indent your code for you. Try
:set autoindent
and watch as vi automatically indents nested blocks of code
like loops and conditional statements. C programmers might also like to take a look at
:set cindent
which offers some additional indenting styles.
2. In
case you forget to turn auto-indenting on, all is not lost. Simply select the lines of code you wish to indent, and hit
=
Vi will do its best to indent the code correctly for
you.
To illustrate the power of this trick, here's a "before" picture:
if(time == 13)
{
lunch();
}
else if (time == 22)
{
if (day == "Friday")
{
echo "Apple pie for dessert!":
}
else
{
echo "Oh no, not lasagna again!";
}
}
and here's the "after" picture:
if(time == 13)
{
lunch();
}
else if (time == 22)
{
if (day == "Friday")
{
echo "Apple pie for dessert!":
}
else
{
echo "Oh no, not lasagna again!";
}
}
3. Newer versions of vi comes with "syntax highlighting" -
the ability to highlight program code in different colours. To turn this feature on, simply type
:syntax on
while
:syntax off
will turn it off.
Vi currently supports most popular
programming languages - your distribution should come with syntax files for C, C++, HTML, Java, Perl, PHP and other common languages.
4. Vi also comes with the very useful "ctags" program, which can be used to create an index of classes, function definitions, variable declarations and methods for C, C++ and Java source code. This index, also known as a "tag" list, can be used by vi and other text editors to assist you in quickly locating sections of your source code while programming.
To use vi's tag features, it's first necessary to create a tag file - simply change to the directory containing your source code and run the "ctags" program on the files you wish to index, like this:
$ ctags *.c
You should now have a file named "tags" in that
directory.
Now open one of your C files, and scroll down until you locate a function call. Position the cursor over that function call, and hit
^-] [that's Ctrl-square-brace]
Vi will automatically load the file containing the function
definition, and display it to you. To go back to the original file, use
^-T [that's Ctrl-T]
This comes in very handy when your code is scattered across
multiple files, and you just can't remember what the function popeye_loves_spinach() does.
This article copyright Melonfire 2000. All rights reserved.