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:

emacs emoji abbrev
emacs emoji abbrev

Call 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

To add a abbrev, type the text you want to expand, then call one of:

For example, suppose you want to define “dir” → “directory”.

  1. Type “directory”.
  2. call add-global-abbrev, in the prompt, type “dir”.

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

If the text is long (for example, “luvm” → “love you very much”), then select it first, and call the add abbrev command with a argument of 0 first.

For example, suppose you want to define “luvm” → “love you very much”.

  1. Type “love you very much”.
  2. Select the text.
  3. Call universal-argumentCtrl+u】, type “0”.
  4. Call add-global-abbrevCtrl+x a g】, in the prompt, type “luvm”.

Now, when you type “luvm” followed by a space, it'll expand to “love you very much”.

Undefine Abbrev

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

For example, to undefine the abbrev “dir”.

  1. Call universal-argumentCtrl+u】, type “-1”.
  2. Call add-global-abbrevCtrl+x a g】, in the prompt, type “dir”.

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

abbrev-file-name

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 also edit abbrevs.

Call edit-abbrevs to edit abbrev.

Emacs will display it like this:

emacs edit abbrev
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
"8in""∈"
"8nin""∉"
"8inf""∞"
"8luv""♥"
"8smly""☺"

;; email
"8me""someone@example.com"

;; computing tech
"8wp""Wikipedia"
"8ms""Microsoft"
"8g""Google"
"8it""IntelliType"
"8msw""Microsoft Windows"
"8win""Windows"
"8ie""Internet Explorer"
"8ahk""AutoHotkey"

;; normal english words
"8alt""alternative"
"8char""character"
"8def""definition"
"8bg""background"
"8kb""keyboard"
"8ex""example"
"8kbd""keybinding"
"8env""environment"
"8var""variable"
"8ev""environment variable"
"8cp""computer"

;; signature
"8xl""Xah Lee"

;; url
"8uxl""http://xahlee.info/"

;; emacs regex
"8d""\\([0-9]+?\\)"
"8str""\\([^\"]+?\\)\""

;; shell commands
 "8ditto""ditto -ck --sequesterRsrc --keepParent src dest"
"8im""convert -quality 85% "

"8f0""find . -type f -size 0 -exec rm {} ';'"
"8rsync""rsync -z -r -v -t --exclude=\"*~\" --exclude=\".DS_Store\" --exclude=\".bash_history\" --exclude=\"**/xx_xahlee_info/*\"  --exclude=\"*/_curves_robert_yates/*.png\" --exclude=\"logs/*\"  --exclude=\"xlogs/*\" --delete --rsh=\"ssh -l xah\" ~/web/ xah@example.com:~/"

Manual Load/Save Abbrev File


Emacs has many other abbrev commands. Here's the list to give you an idea.

Read in emacs manual at (info "(emacs) Abbrevs").

Like it? Buy Xah Emacs Tutorial. Thanks.

or, buy something from my keyboard store.