I imagine that long before now, you have wondered how to turn on all that
magic I said that
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
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:
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
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,
Lets take this one step further. What if we were always spelling “the” as “teh.” We could then create an abbreviation
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
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
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
So, to map
This means that when we press
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
If we want, we can also use shifted function characters. Therefore, we can
map
It has been my experience that, for the most part, if you use
I try not to use the same key sequences that
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.