Emacs: Abbrev Mode by Commands

By Xah Lee. Date: . Last updated: .

The page teaches you about emacs abbrev mode by using the builtin commands.

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 your Emacs Init File:

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

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 you want the abbrev only for the current major mode,

Alt+x add-mode-abbrev
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-abbrev, in the prompt, type “faq”.

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”.

Save Abbrev

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

To auto save, put this in your Emacs Init File:

(setq save-abbrevs 'silently)

Abbrev File Location

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

abbrev-file-name

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

List Abbrevs

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

Edit Abbrevs

Alt+x edit-abbrevs
Edit abbrev. This is the best way to add or remove abbrevs.

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:

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.

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

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 of Lisp code.

Abbrev Mode by Lisp Code

Alternatively, you can use abbrev mode by setting up lisp code. This way, you can control the order of the abbrevs, or grouping them. [see Emacs: Abbrev Mode by Lisp Code]

Emacs: Abbrev Mode

ErgoEmacs mascot-s276x226
Buy Xah Emacs Tutorial

Emacs Tutorial

Quick Start

Font

Split Window

File

Buffer

Find Replace

Copy/Paste

Unicode

Whitespace

Rectangle Edit

Line Wrap

Shell

View Special File

Editing Brackets

Org Mode

HTML

Emacs Efficiency

Misc