Emacs: Abbrev Mode Tutorial

By Xah Lee. Date: . Last updated: .

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

For example:

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

To turn on globally when emacs starts, put this in emacs init:

;; turn on abbrev mode globally
(setq-default abbrev-mode t)

Define Abbrev

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

  1. Type “background”.
  2. call add-global-abbrevCtrl+x a g】, in the prompt, type “bg”.

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

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

add-mode-abbrevCtrl+x a l】 → Define abbrev for current mode.

If the expanded text is more than one word, for example, suppose you want to define “faq” → “frequently asked questions”.

  1. Type “frequently asked questions”.
  2. Select the text.
  3. Alt+x universal-argumentCtrl+u】, type “0”.
  4. Alt+x add-global-abbrevCtrl+x a g】, in the prompt, type “faq”.

Undefine 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. Alt+x universal-argumentCtrl+u】, type “-1”.
  2. Alt+x add-global-abbrevCtrl+x a g】, in the prompt, type “bg”.

Save Abbrev

When you quit, emacs will ask you if you want to save.

Put this in emacs init to auto save:

(setq save-abbrevs 'silently)

Abbrev File Location

The abbrevs is saved in a file at a path specified by the variable


By default, it's at 〔~/.emacs.d/abbrev_defs〕

You can change it, for example, put this in emacs init:

(setq abbrev-file-name "~/emacs_abbre.el")

List Abbrevs

list-abbrevs → Display a list of defined abbrevs.

Edit Abbrevs

You can edit abbrevs. This is the best way to add or remove abbrevs.

Alt+x edit-abbrevs to edit abbrev.

Emacs will display it like this:

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:

Abbrev Example

You can use abbrev for:

I have about 8 hundred abbrevs from all modes. Here's a example:

;; math/unicode symbols
"zin"   0   "∈"
"znin"   0   "∉"
"zinf"   0   "∞"
"zluv"   0   "♥"
"zsmly"   0   "☺"

;; email
"zme"   0   "someone@example.com"

;; computing tech
"zwp"   0   "Wikipedia"
"zms"   0   "Microsoft"
"zg"   0   "Google"
"zit"   0   "IntelliType"
"zmsw"   0   "Microsoft Windows"
"zwin"   0   "Windows"
"zie"   0   "Internet Explorer"
"zahk"   0   "AutoHotkey"

;; normal english words
"zalt"   0   "alternative"
"zchar"   0   "character"
"zdef"   0   "definition"
"zbg"   0   "background"
"zkb"   0   "keyboard"
"zex"   0   "example"
"zkbd"   0   "keybinding"
"zenv"   0   "environment"
"zvar"   0   "variable"
"zev"   0   "environment variable"
"zcp"   0   "computer"

;; signature
"zxl"   0   "Xah Lee"

;; url
"zuxl"   0   "http://xahlee.info/"

;; emacs regex
"zd"   0   "\\([0-9]+?\\)"
"zstr"   0   "\\([^\"]+?\\)\""

;; shell commands
"zf"   0   "find . -type f -size 0 -exec rm {} ';'"

I put “z” in the beginning of my abbrevs. This way, i don't have to worry about clash with short words that i don't want to expand.

Manual Load/Save Abbrev File

Emacs Abbrev Frequently Asked Questions

Make Abbrev Not Add Space

How to make abbrev not add the space?

Define this function:

(defun xah-abbrev-h-f ()
  "Abbrev hook function, used for `define-abbrev'.
 Our use is to prevent inserting the char that triggered expansion. Experimental.
 the “ahf” stand for abbrev hook function.
Version 2016-10-24"

(put 'xah-abbrev-h-f 'no-self-insert t)

And for each of the abbrev that you don't want space added after expansion, add the function there as third argument.

(define-abbrev-table 'global-abbrev-table
    ("addr" "address" xah-abbrev-h-f)
Like it? Buy Xah Emacs Tutorial. Thanks.