logo       Dave's Online Memory
dave's online memory: vi tips You: 38.107.191.85 Friday Mar 12, 2010 5:33AM PST

vi tips - advanced usage

Here are some one-page reference cards that you might find useful. The authors did a pretty good job on each. The originals were postscript, and I've generated pdf versions as well.

Use abbreviations Use the temporary text buffers Assign commands to buffers Use the shell, Luke

Ok, let's assume that you can move around, delete text, save the file, and (most importantly) safely bail out. Now it's time for some real speed tips.

Since a lot of the time, we're typing the same things over and over, it makes sense that any shortcuts we create will save us untold hours. I'll give some examples that were used to create this page.

Use abbreviations

I'm always knocking out new web pages. I use PHP so that I can automate much of the process. As an example, the look 'n feel of this page is kept in a couple of templates in a library directory. A new web page consists of only five lines. Here's a sample. And here's the page that would generate.

Believe it or not, that entire page took only two keystrokes to create. If you have a look at my .exrc file for vi, it contains this line (which has been broken up for readability):


map ;p 1GO<?php^M$title = "";^Minclude "header.php";^M?>^M
^[Go^M<?php include "footer.php"; ?>^[2Gf"

So, all I had to do to create that file (while I was editing this one) was:

  1. :e empty.php
  2. ;p
  3. 5GiEmpty Page^[-2
  4. :w
  5. :rew

Explanation:

  1. From this document, start a new document named "empty.php".
  2. Once in the new, blank document issue the command to insert the template for a new page.
  3. Go to line 5 and insert the text for heading, Empty Page, then escape to command mode and issue a two-key command to turn the line into a 2nd level html heading.
  4. Save the document
  5. Return to this document.

That's an awful lot of production for about three dozen key strokes, eh?

Top of Page
Use the temporary text buffers

Notice the fancy headings? They are created as borderless html tables:

<table class="redbar" width="80%"><tr><td class="redbar">
Use the temporary text buffers
</td></tr></table>

Once I created the first, I realized that I'd be creating more headings, and would be retyping all that code several times. No sweat. I decided to use a temporary buffer to hold the text for future use. Let's see... table... I'll use buffer 't'. So, I moved to the first line, and issued these commands to yank both lines into buffer 't': "t2Y
Now, to create a new red line, I simply do: "tpo
The trailing o opens up a line between them all set to insert the new heading title.

Another use for temporary buffers and oft-used tags is the simple insertion of line breaks and paragraph markerrs. Each paragraph you see is delineated with the tags, <p> and </p> , and line breaks use the tag, <br />. Rather than type them a lot, and since paragraph breaks are usually a good spot to collect one's thoughts, I usually hit the escape key at the end of a paragraph and save the document. (Did I mention that I hit :w after every single change? Yes, I'm paranoid.) Anyway, since I'm already in command mode, and since I've already stored <p> in the 'p' buffer, and </q> in the 'q' buffer, I just hit "qp"pp:w^M. That inserts the closing tag for the current paragraph, the opening tag for the next, and saves the document.

Top of Page
Assign commands to buffers

This document contains several examples of code that contains html entities that must be 'hidden' from the browser to be properly displayed. When inserting html tags, the < and > characters must be converted to &lt; and &gt;. This is just the sort of tedious, repetitive work that temporarily assigned command keys make easy. While explaining how to do this, I'll introduce another trick; type the command directly into the buffer so that you can edit it before executing it.

What did he say? Right, perhaps an example will make more sense. Let's say that we decide that since Ctl-A is unassigned, and since we hardly use the Ctl-B key (what's that for?), we'll assign them as commands to strip all < and > characters on a line.

We open up a line in the document and type (using the key combination, ^V^A (Ctl-V,Ctl-A) to get the literal ^A character, and the same for ^M):
:map ^A :s/</\&lt;/g^M
:map ^B :s/>/\&gt;/g^M

Next, temporarily assign these to a throway buffer that we will execute as though we had typed the command. Arbitrarily, I'll choose 'f'. Yank the first line into buffer f by moving to the line and typing: "fY. Now that buffer 'f' contains the line, we can execute it just as though we had typed it by typing @f. That's right, prepend the @ symbol to a buffer name, and you can execute it. Move down a line and repeat: "fY@f

Now, we can insert a line like this:

<a href="http://foo.com">bar baz</a>

To convert all those 'bad' characters, we simply move to the line and hit ^A and ^B. Voila! The line looks like:

&lt;a href="http://foo.com"&gt;bar baz&lt;/a&gt;

Oh, by the way, don't forget to delete those lines we added...

Top of Page
Use the shell, Luke

One of the strengths of vi, and UN*X in general, is the ability to combine tools to accomplish a task. By using existing system utilities, we can augment our tools to apply to the editing task at hand. An example that often arises whether editing programs, web pages, or email is the need to justify paragraphs of text. This example will work well in a web page, but is aesthetically offensive (among other things):

An example that often arises whether
editing programs, web pages, or email
is the need to justify paragraphs of text. This example will work well in
a web page, but is aesthetically offensive
(among other things):

The UN*X fmt command will justify this text easily, if we can use it. One way would be to quit the editor, pipe the document through fmt like so:

fmt vitips.php > vitips.tmp && mv vitips.tmp vitips.php

But hey, that's an awful lot of work to simply justify a little text. Besides, it'll probably make a mess out of some of the lines that we don't want to wrap. The solution is to decide what area of the file we want to work with, either by line number (remember: set number?), or by assigning bookmarks to the area. Then, pipe them through the external command without ever leaving vi. In this file, those lines are 175 through 179. We could also move to line 175 and mark it. To assign the mark to buffer 'a', move to the line and do a ma. Then move to the last line, 179, and do mb. I use this method usually. I don't have to hunt for line numbers, and 'a,'b is easy to type.

So now that we have the region that we want to work with, call the command with either one of these forms:

:'a,'b !fmt
:175,179!fmt

Note that the space is optional, but I'm superstitious so I insert it, myself.
If we decide that we really want to wrap the lines at column 50, the command would look like:

:'a,'b !fmt -50

The output would look like:

An example that often arises whether editing
programs, web pages, or email is the need to
justify paragraphs of text. This example will
work well in a web page, but is aesthetically
offensive (among other things):

Simple and quick. Want to insert a listing of the current directory, along with the date and time? Do this:

:r!date;ls

Ok, so I lied about the spaces. I vary on that... Anyway, the output looks like this:


Mon Mar  4 22:42:14 EST 2002
empty.php
exrc.php
exrc.txt
vitips.php

Well, that should keep you busy for a while.

enjoy!
...dave

...dave


silly cat image Top of Page FEEDBACK      Comments, Corrections & Questions welcome