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.
In addition to abbreviations, vi also allows you map specific keys, or combinations of keys, to actions with its "map" and "map!" commands. The general format of a key map command [for normal and visual mode] is
:map <key-combo> <action>
while the format [for insert and command mode] is
:map! <key-combo> <action>
At its simplest, a key map is like an abbreviation. For
example, here's one I use frequently when grading papers:
:map! gradef Terrible! This document has no merit whatsoever. I'm awarding you the lowest grade possible in the hope that it will discourage you from attending my class - God grant that I be so lucky!
Ah, Miss Schweppinger - I see you you're familiar with those words. I'm glad they left such a permanent impression on you - and I do applaud your courage in attending today's class. The Kleenex box is in the cupboard behind you.
You can also use the "map!" command to attach commands to specific keys, as in the following example, where I've mapped the Ctrl-H key combination to display help:
:map <C-H> :help<CR>
Now here's something a little more useful for all you Web
developers. I like to map the Ctrl-N key combination to create an empty HTML document, according to a template I've previously defined. Here's what my mapping looks like:
:map <C-N> :read ~/templates/blank.html<CR>
where the file "blank.html" contains a standard HTML
template.
A complete list of all current mappings can be obtained by typing
:map
or
:map!
while a mapped key or key combination can be deleted with the
:unmap <key-combo>
or
:unmap! <key-combo>
commands
You can use key mappings to turn vi into a
truly powerful HTML editor, as Sven Guckes has done on his very interesting page at http://www.math.fu-berlin.de/~guckes/vim/source/html.vim - I suggest you take a look at it sometime.
And now for something truly evil:
:map! a b
:map! e f
:map! i j
:map! o p
:map! u v
I'm sure you see the potential for mayhem - especially if
you're a system administrator with lots of enemies...{mospagebreak title=Vi, Robot!} If automation is your thing, vi is a dream come true; it allows you to record and play back the words you type, and even set or change configuration parameters when pre-defined events take place.
Let's talk about the record and playback functions first. To begin recording, make sure you're in command mode and then type
qa
where "a" is the name of the register to which all keystrokes
will be written [you can use any of the 26 letters of the alphabet as a register identifier]. You'll notice a message at the lower left corner of your screen, indicating that all keystrokes are now being recorded.
Now type this in:
This is a recording. Please leave your message after the beep.
End the recording by typing
q
Now, in order to play it back, position the cursor on a new
line, type
@a
where "a" is the register to be accessed, and watch as vi
repeats your keystrokes for you.
You can precede the playback command with an integer, indicating the number of times it should be executed. So, in the example above, if you used
67@a
you'd get 67 lines, all saying the same thing - similar to a
VCR on magic mushrooms, but not quite as visually entertaining.
Now, what about autocommands? In vi-lingo, an "autocommand" is a command that is executed when creating or editing a new file, reading an existing file, saving a file, or exiting the editor. Autocommands are defined with the "autocmd" command, which usually looks like this:
:autocmd <event> <file-pattern> <command>
or, in accordance with the time-is-money philosophy,
:au <event> <file-pattern> <command>
Vi comes with a whole list of <events> - for example,
"FileReadPost" refers to what happens after a file is read into the editor with the "read" command, while "BufNewFile" is the event that is triggered when a new file is created. A complete list of events can be obtained from the documentation that comes with vi.
The <file-pattern> parameter is simply a wildcard specifying the types of files to run the command on - for example, use *.c for all C source code, or *.html for all HTML documents.
The <command> is the vi command to be automatically executed once both event and file conditions are satisfied. For example,
:au FileReadPost *.txt set ruler
would turn the ruler on each time I read a file with the .txt
extension into the editor.
Or how about this:
:au BufNewFile *.html read ~/template.html
This would ensure that all new files created with a .html
extension would automatically open with the default HTML template.
To display all autocommands in memory, try:
:autocmd
while
:autocmd!
will delete all existing autocommands from memory, leaving
you with a clean slate for subsequent experiments.
This article copyright Melonfire 2000. All rights reserved.