Home arrow Site Administration arrow Page 7 - Vi 101

Searching For Hope - Administration

Vi is probably the most powerful text editor for *NIX, but if you have ever tried to use it, you probably walked away frustrated. This article walks through all the capabilities and features of Vi - from the basics, such as saving and quitting, to the more advanced topics of searching for strings.

  1. Vi 101
  2. Vi? V who?
  3. Start Me Up!
  4. Let's Tango!
  5. Lather, Rinse, Repeat...
  6. One, Two, Buckle My Shoe...
  7. Searching For Hope
  8. Of Files And Windows
  9. Saved By The Bell
By: Vikram Vaswani, (c) Melonfire
Rating: starstarstarstarstar / 5
March 27, 2000

print this article



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?

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:

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

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...

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

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

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

and to get to page 137, simply type

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"

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

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

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

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

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

8. Insert the contents of buffer "p"

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 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: