You can define abbreviations that vi will automatically expand into the full text whenever you type the abbreviation during text-input mode. To define an abbreviation, use the ex command:
:ab
abbr phrase
abbr is an abbreviation for the specified phrase . The sequence of characters that make up the abbreviation will be expanded during text-input mode only if you type it as a full word; abbr will not be expanded within a word. [I abbreviate Covnex to Convex , my company's name, because I have dyslexic fingers. -TC ]
Suppose you want to enter text that contains a phrase that occurs frequently, such as a difficult product or company name. The command:
:ab ns the Nutshell Handbook
abbreviates the Nutshell Handbook to the initials ns . Now whenever you type ns as a separate word during text-input mode, ns expands to the full text.
Abbreviations expand as soon as you press a non-alphanumeric character (e.g., punctuation), a carriage return, or ESC (returning to command mode). [1] When you are choosing abbreviations, choose combinations of characters that don't ordinarily occur while you are typing text. If you create an abbreviation that ends up expanding in places where you don't want it to, you can disable the abbreviation by typing:
[1] An abbreviation won't expand when you type an underscore (
_
); it's treated as part of the abbreviation.
:unab
abbr
To list your currently defined abbreviations, type:
:ab
The characters that compose your abbreviation cannot also appear at the end of your phrase. For example, if you issue the command:
:ab PG This movie is rated PG
you'll get the message
No tail recursion
, and the abbreviation won't be set. The message means that you have tried to define something that will expand itself repeatedly, creating an infinite loop. If you issue the command:
:ab PG the PG rating system
you may or may not produce an infinite loop, but in either case you won't get a warning message. For example, when the above command was tested on a System V version of UNIX, the expansion worked. On a Berkeley version though, the abbreviation expanded repeatedly, like this:
the the the the the ...
until a memory error occurred and vi quit. We recommend that you avoid repeating your abbreviation as part of the defined phrase.
-
,
![]() |
![]() |
![]() |
30.30 vi Outsmarts Dual-Function Function Keys |
![]() |
30.32 Using vi Abbreviations as Commands (Cut and Paste Between vi's) |