Home arrow Site Administration arrow Page 4 - Vi 201

Sweet Revenge - Administration

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.

  1. Vi 201
  2. Liar, Liar...
  3. Re-thinking Relativity
  4. Sweet Revenge
  5. The Shell Game
  6. First Aid 101
  7. An Indent In Time...
  8. Exit Flootburger
By: Vikram Vaswani, (c) Melonfire
Rating: starstarstarstarstar / 4
May 11, 2000

print this article


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


while a mapped key or key combination can be deleted with the

:unmap <key-combo>

:unmap! <key-combo>

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

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

Now, in order to play it back, position the cursor on a new line, type

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

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:


will delete all existing autocommands from memory, leaving you with a clean slate for subsequent experiments.

This article copyright Melonfire 2000. All rights reserved.

>>> More Site Administration Articles          >>> More By Vikram Vaswani, (c) Melonfire

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Coding: Not Just for Developers
- To Support or Not Support IE?
- Administration: Networking OSX and Win 7
- DotNetNuke Gets Social
- Integrating MailChimp with Joomla: Creating ...
- Integrating MailChimp with Joomla: List Mana...
- Integrating MailChimp with Joomla: Building ...
- Integrating MailChimp with Joomla
- More Top WordPress Plugins for Social Media
- Optimizing Security: SSH Public Key Authenti...
- Patches and Rejects in Software Configuratio...
- Configuring a CVS Server
- Managing Code and Teams for Cross-Platform S...
- Software Configuration Management
- Back Up a Joomla Site with Akeeba Backup

Developer Shed Affiliates


Dev Shed Tutorial Topics: