Emacs: Abbrev Mode

By Xah Lee. Date: . Last updated: .

What is Abbrev Mode

Emacs's abbrev feature lets you type a short word and expand into a full word or code template.

For example:

Turn on Abbrev Mode

Alt+x abbrev-mode to turn it on. Call again to turn off.

Define Abbrev

Suppose you want to define “bg” → “background”.

  1. Type “background”.
  2. Alt+x add-global-abbrev, in the prompt, type “bg”.

Now, when you type “bg” followed by a space or return, it'll expand to “background”.

If the expanded text is more than one word, make a text selection first.

If you want the abbrev only for the current major mode,

Alt+x add-mode-abbrev
Define abbrev for current Major Mode only.
Alt+x inverse-add-mode-abbrev
Define global abbrev, but use current word as abbrev, and you type the expansion in prompt.

Remove Abbrev

To remove a definition, give a negative argument to add-global-abbrev or add-mode-abbrev.

For example, to undefine the abbrev “bg”.

  1. universal-argumentCtrl+u】, type “-1”.
  2. Alt+x add-global-abbrev, in the prompt, type “bg”.

List Abbrevs

Alt+x list-abbrevs
Display a list of defined abbrevs.

Edit Abbrevs

Alt+x edit-abbrevs
Edit abbrev.
emacs edit abbrev 65513
emacs edit abbrev screen.

The number in the middle column is the number of times you've used (expanded) the abbrev.

When done, to load and or save, call any of:

edit-abbrevs-redefine
Redefine abbrevs according to current buffer contents.
abbrev-edit-save-buffer
Redefine and save to abbrev file.
abbrev-edit-save-to-file
Redefine and save to abbrev file, but ask for new location.

Manual Load/Save Abbrev File

read-abbrev-file
Read abbrev definitions from file written with write-abbrev-file.
write-abbrev-file
Write all user-level abbrev definitions to a file.

Abbrev Example

You can use abbrev for:

Emacs, name completion

Emacs, using abbrev mode

Emacs abbrev