chapters/08.markdown @ c639d2c11892
typo-the-then
Close branch.
author |
Steve Losh <steve@stevelosh.com> |
date |
Fri, 07 Oct 2016 13:11:30 +0000 |
parents |
b0ca11bfb7a8 |
children |
e582e8d6c46d |
Abbreviations
=============
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:
:::vim
:iabbrev adn and
Now enter insert mode and type:
:::text
One adn two.
As soon as you hit space after typing the `adn` Vim will replace it with `and`.
Correcting typos like this is a great use for abbreviations. Run these
commands:
:::vim
:iabbrev waht what
:iabbrev tehn then
Now enter insert mode again and type:
:::text
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.
Keyword Characters
------------------
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:
:::vim
:set iskeyword?
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":
* The underscore character (`_`).
* All alphabetic ASCII characters, both upper and lower case, and their accented
versions.
* Any characters with an ASCII value between 48 and 57 (the digits zero through
nine).
* Any characters with an ASCII value between 192 and 255 (some special ASCII
characters).
If you want to read the *full* description of this option's format you can check
out `: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.
More Abbreviations
------------------
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:
:::vim
:iabbrev @@ steve@stevelosh.com
: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.
Why Not Use Mappings?
---------------------
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:
:::vim
:inoremap ssig -- <cr>Steve Losh<cr>steve@stevelosh.com
This is a *mapping* intended to let you insert your signature quickly. Try it
out by entering insert mode and typing `ssig`.
It seems to work great, but there's a problem. Try entering insert mode and
typing this text:
:::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:
:::vim
:iunmap ssig
:iabbrev ssig -- <cr>Steve Losh<cr>steve@stevelosh.com
Now try out the abbreviation again.
This time Vim will pay attention to the characters before and after `ssig` and
only expand it when we want.
Exercises
---------
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.