An Emacs abbrev is a special word defined by a particular string. When an abbrev is typed in a buffer (and Abbrev mode is turned on), the abbrev is expanded or replaced by the string it's defined by.
Abbrev mode (a minor mode) allows you to make quick shorthand for long strings or phrases, but you can probably think of other ways that you can use it, too.
The easy way to add abbrevs is to run one of the inverse-add abbrev functions,
inverse-add-local-abbrev. These functions let you
define a word in the buffer as an abbrev; the first applies the abbrev to any
buffer you open in your current Emacs session, and the second defines the abbrev
only to buffers with the same major mode as the current buffer. The latter is
useful for defining abbrevs that are only appropriate to certain modes, such as
for long variable names in a buffer containing program source code.
Try defining an abbrev so that it works across all buffers:
On the next new line, type an abbreviated word,
li,so that point is at the end of the word (after the i).
inverse-add-global-abbrevfunction by typing
C-x a i g.
Define your abbrev where prompted in the minibuffer: Type
Little lamband press Enter.
Notice that the abbrev you type in the buffer is replaced by its definition and point moved to the beginning of the definition.
Now move point to a new line and make a new abbrev the way you just did, by
x (abbrevs aren't case-sensitive) and using
He is to define it.
You've defined two abbrevs. However, as you can see from the mode line, Abbrev
mode is off. Turn it on: Type
M-x abbrev-mode. Erase
those two lines with the definitions you just made and then type the code from
Listing 1. Sample lines with abbrevs
Li, I'll tell thee, Li, I'll tell thee: x called by thy name, For he calls himself a lamb. x meek, and x mild;
The abbrevs for
expand as you type them so, when you're done, the buffer looks like
Figure 3. Abbrevs expanded in an Emacs buffer
This example showed how to define abbrevs that work in all buffers. To define
an abbrev so that it only works in buffers set to the current mode, use
C-x a i
You can also define abbrevs for a single word in the buffer. This is particularly useful when you're writing program source code and you've just typed a long variable.
To define an abbrev for a word, use
C-x a g when
point is after the word. Once you've done that, you're prompted in the
minibuffer for the abbrev to replace that word when Abbrev mode is on.
Try it now:
He became a little childon a new line.
When point is immediately after the word child, type
C-x a g.
cin the minibuffer and press Enter.
End the current line with a period and then type a few more lines to watch the expansion occur:
. We are called by his name. I a c, and thou a lamb,
Notice that Emacs recognizes the c followed by a comma as an abbrev, but not the c in called.
Likewise, to define an abbrev in such a way that it applies only to the current
major mode, use
C-x a l.
To kill all the abbrevs you've defined in your session, use the
Try it: Type
Now none of the abbrevs you've defined (
c) expand to their
definitions, in any buffer, regardless of the mode. Type two more lines to
Little lamb, God bless thee! Little lamb, God bless thee!