Administration
  Home arrow Administration arrow Page 7 - Vi 101
Dev Shed Forums 
Administration  
AJAX  
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 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
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 101
By: Vikram Vaswani, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 5
    2000-03-27

    Table of Contents:
  • Vi 101
  • Vi? V who?
  • Start Me Up!
  • Let's Tango!
  • Lather, Rinse, Repeat...
  • One, Two, Buckle My Shoe...
  • Searching For Hope
  • Of Files And Windows
  • Saved By The Bell

  • 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 101 - Searching For Hope


    (Page 7 of 9 )


    Vi also comes with powerful search and replace features, both based largely on regular expressions. Unfortunately, regular expressions are not part of this course of study; they will be covered in "Reg-ex 101", and so I'm going to restrict myself to an explanation of vi's string search functions, which can be accessed with the / and ? commands.

    Let's say that you've written a treatise on the role played by Mark Hamil in "Star Wars: A New Hope" - except that, when proof-reading your golden prose, you find that you've mistakenly spelt the word "hope" as "dope" in different places in your document. Obviously, you can't ignore this error - it would lead to a completely new interpretation of the planet's favourite sci-fi flick. What do you do?

    /dope
    The /pattern command tells vi to search the document for the specified pattern; once it finds it, it will position the cursor at the first character of the string, and await further commands.

    To search backwards, use the ?pattern command, like this:

    ?dope
    It's unlikely - not to mention naïve on your part - to assume that this error would only have occurred once. You can repeat the last search by pressing

    n
    or, in vi lingo, "next match".

    It isn't enough to just identify your mistake - the next step, according to most major religious doctrines and television preachers, is to repent and make sure it doesn't happen again. Vi can help you there too - although it will involve memorizing a command which, at first glance, is almost enough to make you wonder if there really is a God...

    :1,$s/pattern_to_find/pattern_to_replace/g
    In case you're wondering where the "s" came from - it's vi-talk for "substitute". The 1 and the $ are symbols indicating the range within which the substitution is to take place - in this case, from the beginning of the document [line 1] to the end [$]. The "g" is a flag indicating that *all* matches should be replaced.

    And so, in the above example, the command to replace the word "dope" with "hope" would be

    :1,$s/dope/hope/g
    Now, if only getting Leia's phone number was this easy...

    This article copyright Melonfire 2000. All rights reserved.{mospagebreak title=Bookmarks, Buffers and Beach Bunnies}
    Vi also allows you to define "bookmarks" within your document - this makes it possible to easily jump to and between specific sections of large files. As an example, consider the following blocks of text:

    ...this document outlines the proposed corporate structure for Muscle Cars, Inc., a company engaged in the business of manufacturing and selling...[page 2]

    ...the CEO and President of Muscle Cars, Inc., James van Hausen, who, in his misbegotten childhood, was best-known for wrapping expensive sports cars around trees, has succeeded in...[page 137]

    So let's set a couple of bookmarks, shall we? On page 2, move your cursor to the word "document", and type

    ma
    This sets a bookmark, identified by the letter "a", at the cursor position. Now find your way to page 137, and set a bookmark named "b" at the word "cars". To switch back to page 2, all you now need to do is type

    `a
    and to get to page 137, simply type

    `b
    Any lowercase letter can be used as a bookmark identifier. Obviously, you're limited to a total of 26 bookmarks per file - but in most cases, that's more than enough. If you need more than that, I strongly suggest you try therapy.

    In addition to multiple bookmarks, vi also supports multiple buffers. If you've been paying attention, you know that all the material you delete or copy finds its way to a temporary buffer until it's either replaced or removed. But vi also has "named buffers" - similar to a set of storage lockers, these buffers can be used to store different blocks of text, and make them available for insertion whenever required.

    As an example, take a look at this section of an essay handed in to me by one of my former students:

    ...beach bunnies seem to have a pretty good life of it. All they seem to do is lounge around on the beach, drinking pina coladas and slathering suntan lotion all over themselves. Once in a while, they get up and toss a volleyball around. Sounds like fun - where do I sign up?...

    I'm sure you understand why I said "former student" now...

    Now let's suppose that I wanted to transpose the phrases "drinking pina coladas" and "slathering suntan lotion". Here's what I would do:
    1. Move my cursor to the beginning of the word "drinking"

    2. Delete the three words, and move them to a buffer named "p"

    "p3dw
    3. Move my cursor to the beginning of the word "slathering"

    4. Delete those three words, and move them to a buffer named "s"

    "s3dw
    5. Move my cursor back to before the word "and"

    6. Insert ["put"] the contents of buffer "s"

    "sp
    7. Move my cursor to after the word "and"

    8. Insert the contents of buffer "p"

    "pp
    9. Congratulate myself loudly and exuberantly, as I'm doing right now!

    As you can see, to use a buffer, simply precede the regular copy/delete/replace command with the buffer identifier. A buffer identifier consists of a double-quote mark, followed by a lowercase letter. In the example above, both "s and "p are buffer identifiers.

    I should add here that neither bookmarks nor buffer contents are retained once you exit the editor.

    This article copyright Melonfire 2000. All rights reserved.

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


     

       

    ADMINISTRATION ARTICLES

    - Network Administration with FreeBSD 7
    - Components of an Information Architecture
    - The Anatomy of an Information Architecture
    - 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





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