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:

Here's how to set up.

Defining Your Abbrevs

Create a file with the following content:

;; sample use of emacs abbreviation feature

(define-abbrev-table 'global-abbrev-table '(

    ;; 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:~/")

;; stop asking whether to save newly added abbrev when quitting emacs
(setq save-abbrevs nil)

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

Now, put the above file at ~/.emacs.d/my_emacs_abbrev.el. Now, in your emacs init file, put this line:

(load "my_emacs_abbrev")

You can now restart emacs, or just select the line then call eval-region. That will load your abbrev definitions.

Now, type for example 8g then press Space, then it'll expand to Google .

I add “8” in front of all my abbrevs. This way, it is easy to avoid expanding when you don't want to. I choose “8” because it's one of the easy reach keys for right hand, and is a rare starting letter of a word.

Add / Remove Abbrev

To add a new abbrev, just create a new line of definition, then call eval-buffer.

To remove, just remove the line.

Add the abbrev file to Emacs's Bookmark, so you can open it fast.

Emacs's Method for Working with Abbrevs

It's not necessary to create lisp code for abbrev as shown on this page.

Emacs has many commands for working with abbrevs interactively.

You can read about it at: (info "(emacs) Abbrevs").

Like it? Buy Xah Emacs Tutorial. Thanks.

or, buy something from my keyboard store.