Administration
  Home arrow Administration arrow Page 4 - Vi 201
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ADMINISTRATION

Vi 201
By: Vikram Vaswani, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 4
    2000-05-11

    Table of Contents:
  • Vi 201
  • Liar, Liar...
  • Re-thinking Relativity
  • Sweet Revenge
  • The Shell Game
  • First Aid 101
  • An Indent In Time...
  • Exit Flootburger

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Vi 201 - Sweet Revenge
    (Page 4 of 8 )

    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.

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


     

       

    ADMINISTRATION ARTICLES

    - Configuring Load-Balanced Clusters
    - Load-Balanced Clusters
    - UNIX Time Format Demystified
    - Making Changes in the CVS
    - Building Your First CVS Repository
    - CVS Quickstart Guide
    - Authorizing Users in Samba
    - Handling User Accounts in Samba
    - Authentication in Samba
    - Accounts, Authentication, and Authorization
    - Advanced Concepts on Dealing with Files and ...
    - Dealing with Files and Filesystems
    - More Hacks for the User Environment in BSD
    - Personalizing the User Environment in BSD
    - Customizing the User Environment in BSD

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway