Site Administration Page 4 - Vi 201 |
See Also: 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 while the format [for insert and command mode] is At its simplest, a key map is like an abbreviation. For example, here's one I use frequently when grading papers: 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: 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: where the file "blank.html" contains a standard HTML template. A complete list of all current mappings can be obtained by typing or while a mapped key or key combination can be deleted with the or 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: 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: 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: or, in accordance with the time-is-money philosophy, 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, would turn the ruler on each time I read a file with the .txt extension into the editor. Or how about this: 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: while will delete all existing autocommands from memory, leaving you with a clean slate for subsequent experiments. This article copyright Melonfire 2000. All rights reserved.
blog comments powered by Disqus |