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:
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,
, , or , 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
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 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.
. This tellsWe 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
(for write). If I want to save the file and quit, I enter 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
So, to map
so that every time we press it, we write our current file to disk, the command would be
This means that when we press
vi interprets it as though we actually typed and pressed Enter (the : ). 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
, then . It looks like this:(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
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
and with non-function keys, vi only sees and not . Also, may not work because on the system console, 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.