26.3. Controlling Abbrev Expansion

An abbrev expands whenever it is present in the buffer just before point and you type a self-inserting whitespace or punctuation character (SPC, comma, etc.). More precisely, any character that is not a word constituent expands an abbrev, and any word-constituent character can be part of an abbrev. The most common way to use an abbrev is to insert it and then insert a punctuation character to expand it.

Abbrev expansion preserves case; thus, foo expands into find outer otter; Foo into Find outer otter, and FOO into FIND OUTER OTTER or Find Outer Otter according to the variable abbrev-all-caps (a non-nil value chooses the first of the two expansions).

These commands are used to control abbrev expansion:

M-'

Separate a prefix from a following abbrev to be expanded (abbrev-prefix-mark).

C-x a e

Expand the abbrev before point (expand-abbrev). This is effective even when Abbrev mode is not enabled.

M-x expand-region-abbrevs

Expand some or all abbrevs found in the region.

You may wish to expand an abbrev with a prefix attached; for example, if cnst expands into construction, you might want to use it to enter reconstruction. It does not work to type recnst, because that is not necessarily a defined abbrev. What you can do is use the command M-' (abbrev-prefix-mark) in between the prefix re and the abbrev cnst. First, insert re. Then type M-'; this inserts a hyphen in the buffer to indicate that it has done its work. Then insert the abbrev cnst; the buffer now contains re-cnst. Now insert a non-word character to expand the abbrev cnst into construction. This expansion step also deletes the hyphen that indicated M-' had been used. The result is the desired reconstruction.

If you actually want the text of the abbrev in the buffer, rather than its expansion, you can accomplish this by inserting the following punctuation with C-q. Thus, foo C-q , leaves foo, in the buffer.

If you expand an abbrev by mistake, you can undo the expansion and bring back the abbrev itself by typing C-_ to undo (Section 7.4). This also undoes the insertion of the non-word character that expanded the abbrev. If the result you want is the terminating non-word character plus the unexpanded abbrev, you must reinsert the terminating character, quoting it with C-q. You can also use the command M-x unexpand-abbrev to cancel the last expansion without deleting the terminating character.

M-x expand-region-abbrevs searches through the region for defined abbrevs, and for each one found offers to replace it with its expansion. This command is useful if you have typed in text using abbrevs but forgot to turn on Abbrev mode first. It may also be useful together with a special set of abbrev definitions for making several global replacements at once. This command is effective even if Abbrev mode is not enabled.

Expanding an abbrev runs the hook pre-abbrev-expand-hook (Section 32.2.3).