Vim Abbreviations
Vim abbreviations can be used to automatically expand words or phrases which you commonly use. It can also be used to correct common spelling mistakes.
Use the abbreviate command to define abbreviations, the command can be shortened as ab. Abbreviations work in the insert and command-line modes.
Define the following abbreviations.
1:ab teh the
2:ab jdm john_doe@gmail.com
Now type “teh” followed by a space, it will autocorrect to “the”. Similarly, “jdm” will expand to “john_[email protected]”.
Vim does not expand the abbreviation as soon as you type it. It will do so only if you type a non-keyword character immediately after the abbreviation. Space, dot, comma, semi-colon, etc are non-keyword characters. Almost all characters that tend to indicate the end of a word are non-keyword characters.
tip: Sometimes you may not want a non-keyword character to be inserted after the replacement text. Use the shortcut Ctrl+] as an expansion trigger. Only some complex abbreviations tend to benefit from this shortcut. We will see an example of this later.
Types of abbreviations
We have seen that to trigger an abbreviation expansion, we must type a non-keyword character after the abbreviation. Vim has similar but more complex rules regarding which type of character can appear immediately before the abbreviation. The rules depend on the type of abbreviation.
Abbreviations are classified based on the type of characters that are permitted in it.
1. Full-id
Full-id abbreviations consist entirely of keyword characters. In simple words, keyword characters mean alphanumeric characters and the underscore. It also includes some special symbols.
examples: “the”, “if_main”, “mail2”
Vim will expand a full-id abbreviation only if it is immediately preceded by a non-keyword character. So you don’t need to worry about your abbreviations appearing as part of another word. You can type something like “statehood” after running the :ab teh the
command. The “teh” inside “statehood” will not trigger an abbreviation expansion. Full-id abbreviation expansion work as you expect most of the time.
For most use cases full-id abbreviations are sufficient. It is okay to skim over the other two.
2. End-id
End-id abbreviations must have exactly one keyword character and it must be the last character. All other characters must be non-keyword.
examples: “:f”, “##1”
End-id abbreviations are expanded only if they are preceded by a keyword character, a space, or a tab.
3. Non-id
Non-id abbreviations end in a non-keyword character. Any type of character can appear before that.
examples: “+++”, ” a/”
Non-id abbreviations are expanded only if they are preceded by a space or a tab.
Rules common to all abbreviations
Two rules regarding the preceding character are applicable to all abbreviations.
Rule 1: Any abbreviation can appear at the start of a line.
Rule 2: If the first characters we type after entering the mode is an abbreviation, the preceding character does not matter.
Example:
- Define the
:ab teh the
abbreviation which we saw before. - Type character “t”.
- Exit insert mode by pressing escape.
- Re-enter insert mode by pressing “a”.
- Type “teh”.
- The abbreviation will be expanded despite the fact that we typed “tteh”.
Mode-specific abbreviations
The abbreviate command works in both insert mode and on the command line. Vim also has an iabbrev command which works only in insert mode and a cabbrev command which works only on the command line. Unless required, it is better to use mode-specific abbreviation commands.
iabbrev is the more useful of the two commands but cabbrev has its uses as well. For example, you can abbreviate paths to commonly used files. It is useful for testing Vim script functions. If you are someone inclined to have a large number of shortcuts and abbreviations, the fact cabbrev does not conflict with normal mode or insert mode is a big plus.
Preventing abbreviation expansion
To prevent abbreviation expansion type Ctrl+V before typing the non-keyword trigger character. In Windows, you may have to use Ctrl+Q depending on your Vim settings. This takes care of the rare situations were abbreviation expansion is not wanted. For example, to redefine a command mode or mode agnostic abbreviation.
Listing abbreviations
Go to the command line and type the ab[breviate] command without any parameters.
:ab
This will list all your defined abbreviations.
Removing abbreviations
Use the una[abbreviate] command to remove a single abbreviation.
:una teh
Use iuna[bbrev] for removing insert mode abbreviations and cuna[bbrev] for removing command-line mode abbreviations.
To remove all abbreviations use the abc[lear] command.
:abc
Similarly, use iabc[lear] to remove all insert mode abbreviations and cabc[lear] to remove all command-line mode abbreviations.
Expression Abbreviation
You are not limited to replacing an abbreviation with a constant value. Expression abbreviations allow you to specify a Vim expression as a replacement for the abbreviation. Vim will evaluate the expression and replace the abbreviation with its value. To tell Vim to treat the replacement text as an expression, put the keyword
Example:-
:iabbrev <expr> instm strftime('%c')
Vim will replace “instm” with the current date and time. I got 2020-07-29 4:43:45 PM as the replacement. Any valid Vim expression can be used this way.
To insert a constant value and an expression, use the
iabbrev instm Date:<C-R>=strftime('%c')<cr>
For more details please see :help "=
Complex Abbreviations
Vim abbreviations can insert non-printing characters like newline, up arrow, down arrow, tab, etc. You can even enter normal mode by using the escape key, execute some normal mode commands, and come back to insert mode via “i”, “I”, “a”, “A”, etc. From normal mode, you can also go to the command line mode by using a colon. To execute a command line mode command, follow the command with a carriage return.
iabbrev prcd <pre><code></code></pre><Esc>2F<i
This inserts an HTML code tag inside a pre tag. The escape takes you to normal mode and the “2F<” places the cursor between the closing and opening tags of the code element. The “i” brings you back to insert mode. In this example, the Ctrl+] shortcut makes sense because you don’t want an unnecessary non-keyword character between the opening and closing tags.
Buffer local abbreviations
Abbreviations can be local to a buffer. Open a file in Vim and type the following command.
:ab <buffer> foo bar
This abbreviation will be available only in the current buffer. At first glance, this may not seem very promising. But it is very useful when creating filetype plugin files or using Autocommands.
Create the following autocommand.
au FileType python :iabbrev ifmain if __name__ == '__main__':
This abbreviation will work only in python files.