vi Magic

vi Magic

I imagine that long before now, you have wondered how to turn on all that magic I said that vi could do. Okay, let’s do it.

The first thing I want to talk about is abbreviations. You can tell vi that when you type in a specific set of characters it is supposed to automagically change it to something else. For example, we could have vi always change “USA” to “United States of America”. This is done with the


To create a new abbreviation, you must get into ex-mode by pressing the colon (:) in command mode. Next, type in abbr, followed by what you want to type in, and what vi should change it to. For example:

:abbr USA United States of America

Note that the abbreviation cannot contain any spaces because vi interprets everything after the second word as being part of the expansion.

If we later decide we don’t want that abbreviation anymore, we enter

:unabbr USA

Because it is likely that we will want to use the abbreviation USA, it is not a good idea to use an abbreviation that would normally occur, such as USA. It would be better, instead, to use an abbreviation that doesn’t occur normally, like Usa. Keep in mind, that abbreviations only apply to complete words. Therefore, something like the name “Sousa” won’t be translated to “SoUSA.” In addition, when your abbreviation is followed by a space, Tab, Enter, or Esc, the change is made.

Lets take this one step further. What if we were always spelling “the” as “teh.” We could then create an abbreviation

:abbr teh the

Every time we misspell “the” as “teh,” vi would automatically correct it. If we had a whole list of words that we regularly misspelled and created similar abbreviations, then every time we entered one of these misspelled words, it would be replaced with the correctly spelled word. Wouldn’t that be automatic spell correction?

If we ever want to “force” the spelling to be a particular way (that is, turn off the abbreviation momentarily), we simply follow the abbreviation with a Ctrl-V. This tells vi to ignore the special meaning of the following character. Because the next character is a white space, which would force the expansion of the abbreviation (which makes the white space special in this case), “turning off” the white space keeps the abbreviation from being expanded.

We can also use vi to re-map certain sequences. For example, I have created a command so that all I need to do to save a file is Ctrl-W (for write). If I want to save the file and quit, I enter Ctrl-X with the “map” command.

The most common maps that I have seen have used control sequences, because most of the other characters are already taken up. Therefore, we need to side-step a moment. First, we need to know how to access control characters from within vi. This is done in either command mode or input mode by first pressing Ctrl-V and then pressing the control character we want. So to get Ctrl-W, you would type Ctrl-V, then Ctrl-W. This would appear on the screen as ^W. This looks like two characters, but if you inserted it into a text file and moved over it with the cursor, you would realize that vi sees it as only one character. Note that although I pressed the lowercase w, it will appear as uppercase on the screen.

So, to map Ctrl-W so that every time we press it, we write our current file to disk, the command would be

map ^W :w^M

This means that when we press Ctrl-W, vi interprets it as though we actually typed :w and pressed Enter (the Ctrl-M: ^M). The enter key at the end of the command is a good idea because you usually want the command to be executed right away. Otherwise, you would have to press Enter yourself.

Also keep in mind that this can be used with the function keys. Because I am accustomed to many Windows applications in which the F2 key means to save, I map F2 to Ctrl-V, then F2. It looks like this:

map ^[[N :w^M (The ^[[N is what the F2 key displays on the screen)

If we want, we can also use shifted function characters. Therefore, we can map Shift-F2 to something else. Or, for that matter, we can also use shifted and control function keys.

It has been my experience that, for the most part, if you use Shift and Ctrl with non-function keys, vi only sees Ctrl and not Shift. Also, Alt may not work because on the system console, Alt plus a function key tells the system to switch to multiscreens.

I try not to use the same key sequences that vi already does. First, it confuses me because I often forget that I remapped something. Second, the real vi commands are then inaccessible. However, if you are used to a different command set (that is, from a different editor), you can “program” vi to behave like that other editor.

Never define a mapping that contains its own name, as this ends up recursively expanding the abbreviation. The classic example is :map! n banana. Every time you typed in the word “banana,” you’d get


and depending on what version you were running, vi would catch the fact that this is an infinite translation and stop.