Vim has a feature called "abbreviations" that feel similar to mappings but are meant for use in insert, replace, and command modes. They're extremely flexible and powerful, but we're just going to cover the most common uses here.
We're only going to worry about insert mode abbreviations in this book. Run the following command:
:iabbrev adn and
Now enter insert mode and type:
One adn two.
As soon as you hit space after typing the
adn Vim will replace it with
Correcting typos like this is a great use for abbreviations. Run these commands:
:iabbrev waht what :iabbrev tehn then
Now enter insert mode again and type:
Well, I don't know waht we should do tehn.
Notice how both abbreviations were substituted, even though you didn't type a space after the second one.
Vim will substitute an abbreviation when you type any "non-keyword character"
after an abbreviation. "Non-keyword character" means any character not in the
iskeyword option. Run this command:
You should see something like
iskeyword=@,48-57,_,192-255. This format is
very complicated, but in essence it means that all of the following are
considered "keyword characters":
If you want to read the full description of this option's format you can check
:help isfname, but I'll warn you that you'd better have a beer at the
ready for this one.
For our purposes you can simply remember that abbreviations will be expanded when you type anything that's not a letter, number, or underscore.
Abbreviations are useful for more than just correcting typos. Let's add a few more that can help in day-to-day text editing. Run the following commands:
:iabbrev @@ firstname.lastname@example.org :iabbrev ccopy Copyright 2013 Steve Losh, all rights reserved.
Feel free to replace my name and email address with your own, then enter insert mode and try them out.
These abbreviations take large chunks of text that you type often and compress them down to a few characters. Over time, this can save you a lot of typing, as well as wear and tear on your fingers.
If you're thinking that abbreviations seem similar to mappings, you're right. However, they're intended to be used for different things. Let's look at an example.
Run this command:
:inoremap ssig -- <cr>Steve Losh<cr>email@example.com
This is a mapping intended to let you insert your signature quickly. Try it
out by entering insert mode and typing
It seems to work great, but there's a problem. Try entering insert mode and typing this text:
Larry Lessig wrote the book "Remix".
You'll notice that Vim has expanded the
ssig in Larry's name! Mappings don't
take into account what characters come before or after the map -- they only look
at the specific sequence that you mapped to.
Remove the mapping and replace it with an abbreviation by running the following commands:
:iunmap ssig :iabbrev ssig -- <cr>Steve Losh<cr>firstname.lastname@example.org
Now try out the abbreviation again.
This time Vim will pay attention to the characters before and after
only expand it when we want.
Add abbreviations for some common typos you know you personally make to your
~/.vimrc file. Be sure to use the mappings you created in the last chapter to
open and source the file!
Add abbreviations for your own email address, website, and signature as well.
Think of some pieces of text you type very often and add abbreviations for them too.