Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Vimscript Abbreviations


May 24, 2021 Vim


Table of contents


Vim has a feature called "abbreviations" that is a bit similar to mapping, but it is used in insert, replace, and command modes. This feature is flexible and powerful, but this section only talks about the most common usages.

This book will only tell the story of abbreviations in insert mode. Run the following command:

:iabbrev adn and

Go into insert mode and enter:

One adn two.

After adn and entering the space bar, Vim replaces it with and .

Input correction such as this is a useful use of abbreviations. Run the command:

:iabbrev waht what
:iabbrev tehn then

Go back into insert mode and enter:

Well, I don't know waht we should do tehn.

Note The replacement timing of the two abbreviations, the second without entering spaces, is also replaced.

Keyword Characters

After entering "non-keyword character" immediately after an abbreviation, Vim replaces that abbreviation. " non-keyword character" refers to characters that iskeyword option. Run the command:

:set iskeyword?

You'll see results similar to iskeyword=@,48-57,_,192-255 This format is complex, but essentially "keyword characters" contains several:

  • Underscore characters ( _ ).
  • All letter characters, including case.
  • ASCII values are characters between 48 and 57 (numbers 0-9).
  • ASCII values are characters between 192 and 255 (some special ASCII characters).

If you want to read the full description of this option format, you can run :help isfname and it's best to prepare something to eat before reading.

All you have to remember is that entering non-letter, numeral, underscore characters will trigger abbreviations replacement.

More about abbreviations

Abbreviations can't just correct mistakes. W e can add a few abbreviations commonly used in daily editing. Run the following command:

:iabbrev @@    [email protected]
:iabbrev ccopy Copyright 2013 Steve Losh, all rights reserved.

Feel free to change my name and email address for yours, then try these two abbreviations

These abbreviations compress a long list of characters you often use into a few characters, saveing you the hassle each time.

Why Not Use Mappings? Why not use Mappings?


Yes, abbreviations are very much like mappings, but their positioning is different. Here's an example:

Run the command:

:inoremap ssig -- <cr>Steve Losh<cr>steve@stevelosh.com

This mapping is used to quickly insert your signature. Go into insert mode and enter ssig to try.

Everything seems fine, but there's a problem. Go into insert mode and enter the following text:

Larry Lessig wrote the book "Remix".

Notice that Vim replaced ssig name! Mappings regardless of the front and back characters of the mapped string -- it only looks for the specified string in the text and replaces them.

Run the following command to remove the mappings above and replace it with an abbreviation:

:iunmap ssig
:iabbrev ssig -- <cr>Steve Losh<cr>steve@stevelosh.com

Try this abbreviation again.

This time Vim will pay attention to the front and back characters of ssig and will only replace it when needed.

Exercises

Add abbreviations configuration to frequently misspelled words in your ~/.vimrc file. Be sure to use the mappings you created in the last chapter to reopen the ~/.vimrc file.

Add abbreviations configuration to your email address, blog URL, signature.

Add an abbreviation configuration to the text you enter frequently.