René Nyffenegger's collection of things on the web
René Nyffenegger on Oracle - Most wanted - Feedback -
 

Using autocommands in vim or gvim

Autocommands are a powerful feature used by vim (or gvim) that automatically executes configurable commands when a specifig event takes place. Autocommands make it possible to set up a different editing environment based upon the filetype being edited (because starting to edit a file is one of those detectable events).
An example for an auto command is:
:au BufRead *.txt set shiftwidth=2
This will set the shiftwidth to 2 if a file is loaded (BufRead) and the file name is ending in txt. BufRead is a so called event. You can choose among many events, like BufWrite, FocusGained and many others. You should definitely refer to the documentation to read about all events.

Groups

:autocmd someGroupName BufReadPre *.huh so xyz
When a group (here: someGroupName) is used, it must have previously been defined with :autogroup, otherwise, it causes a E216: No such group or event: xyz BufReadPre *.huh so x.

FileType

A special event is FileType. Whenever a :setfiletype foo (for example perl instead of foo) is executed, it triggers the FileType event. When vim started up, several auto coommands were installed to catch this event and accordingly source the correct syntax and indent file.