Emacs: Abbrev Mode

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:

Most Simple Way of Using Abbrev

Emacs has over 20 abbrev commands and is fairly complex. If you want to learn that, see Emacs: Abbrev Mode Tutorial.

Following is the the most simple and efficient way of using abbrev.

Create a file with the following content:

;; -*- coding: utf-8; lexical-binding: t; -*-
;; emacs abbrev def
;; http://ergoemacs.org/emacs/emacs_abbrev_mode.html

(clear-abbrev-table global-abbrev-table)

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

    ;; phrase
    ("afaik" "as far as i know" )
    ("atm" "at the moment" )
    ("dfb" "difference between" )
    ("ty" "thank you" )

    ("ai" "artificial intelligence" )
    ("btw" "by the way" )

    ("cnt" "can't" )
    ("ddnt" "didn't" )
    ("dnt" "don't" )
    ("dsnt" "doesn't" )
    ("dvp" "develop" )
    ("eg" "e.g." )
    ("ie" "i.e." )
    ("hnt" "haven't" )
    ("isnt" "isn't" )
    ("shnt" "shouldn't" )
    ("wnt" "won't" )
    ("wsnt" "wasn't" )
    ("wtdb" "What's the difference between" )
    ("sa" "See also:" )

    ;; english word abbrev
    ("ann" "announcement" )
    ("arg" "argument" )
    ("autom" "automatic" )
    ("bc" "because" )
    ("bg" "background" )
    ("bt" "between" )
    ("comm" "communication" )
    ("comp" "computer" )
    ("desc" "description" )
    ("dict" "dictionary" )
    ("dir" "directory" )
    ("dirs" "directories" )
    ("disc" "discussion" )
    ("dl" "download" )
    ("eng" "English" )
    ("env" "environment" )
    ("esp" "especially" )
    ("ex" "example" )
    ("fex" "for example," )
    ("fu" "function" )
    ("gvn" "government" )
    ("hex" "hexadecimal" )
    ("ia" "interactive" )
    ("impl" "implementation" )
    ("implt" "implement" )
    ("intn" "international" )
    ("intro" "introduction" )
    ("jp" "Japanese" )
    ("kb" "keyboard" )
    ("kbd" "keybinding" )
    ("kbs" "keyboards" )
    ("kw" "keyword" )
    ("ob" "object" )
    ("oc" "occur" )
    ("occ" "occurrence" )
    ("paren" "parenthesis" )
    ("pl" "programing language" )
    ("pls" "please" )
    ("prof" "professor" )
    ("ref" "reference" )
    ("scs" "screenshot" )
    ("tb" "trackball" )
    ("techn" "technology" )
    ("trad" "traditional" )
    ("ver" "version" )
    ("vid" "video" )
    ("wp" "Wikipedia" )
    ("ualt" "alternative" )
    ("uauto" "automatic" )
    ("uc" "character" )
    ("uchars" "characters" )
    ("udev" "development" )
    ("uergo" "ergonomic" )
    ("uexp" "experience" )
    ("uexpr" "expression" )
    ("ul" "language" )
    ("uorg" "organization" )
    ("upm" "parameter" )
    ("uprog" "programing" )
    ("uq" "question" )

    ("ahd" "American Heritage Dictionary" )

    ;; computing
    ("ahk" "AutoHotkey" )
    ("cfg" "context-free grammar" )
    ("cj" "Clojure" )
    ("cl" "Common Lisp" )
    ("cs" "computer science" )

    ;; tech company
    ("gc" "Google Chrome" )
    ("ge" "Google Earth" )
    ("gh" "Google Hangout" )
    ("gm" "Google Map" )
    ("gp" "Google Plus" )
    ("lsl" "Linden Scripting Language" )
    ("macosx" "Mac OS X" )
    ("mma" "Mathematica" )
    ("ms" "Microsoft" )
    ("msvs" "Microsoft Visual Studio" )
    ("msw" "Microsoft Windows" )
    ("osx" "OS X" )
    ("pp" "PayPal" )
    ("fb" "Facebook" )
    ("sl" "Second Life" )
    ("yt" "YouTube" )
    ("uff" "Firefox" )
    ("uso" "StackOverflow" )

    ;; programing
    ("ev" "environment variable" )
    ("evs" "environment variables" )
    ("faq" "frequently asked questions" )
    ("fs" "fullscreen" )
    ("ipa" "IP address" )
    ("jq" "jQuery" )
    ("jvm" "Java Virtual Machine" )
    ("mac" "Mac" )

    ("rsi" "Repetitive Strain Injury" )
    ("sf" "San Francisco" )

    ("subdir" "sub-directory" )
    ("subdirs" "sub-directories" )

    ("vb" "Visual Basic" )
    ("wd" "web development" )
    ("wm" "Window Manager" )

    ("ucli" "command line interface" )

    ("udb" "database" )
    ("udef" "definition" )
    ("udf" "different" )
    ("udfc" "difference" )
    ("udoc" "documentation" )
    ("udt" "data type" )
    ("uel" "emacs lisp" )

    ("ufl" "functional language" )
    ("ufp" "functional programing" )
    ("ugui" "graphical user interface" )
    ("ujs" "JavaScript" )
    ("ulhs" "left-hand-side" )
    ("ulib" "library" )
    ("ulibs" "libraries" )
    ("umath" "mathematics" )
    ("umd" "metadata" )
    ("uoop" "object oriented programing" )
    ("uos" "operating system" )

    ("urhs" "right-hand-side" )
    ("usc" "source code" )

    ("utemp" "temperature" )
    ("va" "variable" )
    ("vas" "variables" )
    ("nn" "non-nil" )

    ;; programing
    ("eq" "==" )
    ("eqq" "===" )
    ("r" "return" )

    ;; regex
    ("uaz" "\\([A-Za-z0-9]+\\)" )
    ("ubracket" "\\[\\([^]]+?\\)\\]" )
    ("ucurly" "“\\([^”]+?\\)”" )
    ("ud" "\\([0-9]+\\)" )
    ("udate" "\\([0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]\\)" )
    ("udot" "\\(.\\)" )
    ("ustr" "\\([^\"]+?\\)" )
    ("utag" "\\([</>=\" A-Za-z0-9]+\\)" )

    ;; unicode
    ("md" "—" )
    ("uascii" "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~" )

    ("bu" "•" )
    ("catface" "😸" )
    ("hearts" "♥💕💓💔💖💗💘💝💞💟💙💚💛💜" )
    ("omg" "😂" )
    ("la" "←" )
    ("ua" "↑" )
    ("ra" "→" )
    ("da" "↓" )

    ;; code
    ("uutf8" "-*- coding: utf-8 -*-" )

    ("eem" "ergoemacs-mode" )

    ;; url
    ("uemacs" "http://ergoemacs.org/emacs/emacs.html" )

    ("ui" "user interface" )
    ("uns" "understand" )
    ("ur" "you are" )
    ;;
    ))

(set-default 'abbrev-mode t)

(setq save-abbrevs nil)

name the file my-abbrev.el

Save the file at

~/.emacs.d/lisp/my-abbrev.el

Create the directory if it doesn't exist.

Then, in your emacs init file, add the line:

(load "~/.emacs.d/lisp/my-abbrev.el")

Restart emacs.

Now, when you type for example, “btw” followed by a space or punctuation, it'll become “by the way”.

(note: some of the abbrev above start with letter “u”, so that it can prevent collision with simple words. For example, “ugui” instead of “gui” for “graphical user interface”. Because sometimes you want just GUI. Letter “u” is chosen because it's a easy key on keyboard. You can change it to “z” or “3”. )

Edit Abbrev

To edit the abbrev, just open the file, add/remove a line, then call eval-buffer.

It works right away.

Add the abbrev file to your bookmark, so it's fast to open.

〔►see Emacs: Bookmark

Prevent Abbrev Expansion

To stop abbrev from expansion, press Ctrl+q before typing space or punctuation.

Abbrev Word Tips

Abbrev cannot contain punctuation. (you cannot change this, it's part of C code.)

This is actually good because punctuation are harder to type.

Emacs Abbrev Commands

What we covered above is the most simple and efficient way to use abbrev.

Emacs has over 20 abbrev commands and is fairly complex. If you want to learn emacs's traditional way, see

Emacs: Abbrev Mode Tutorial

Like it? Buy Xah Emacs Tutorial. Thanks.

or, buy something from Best Keyboard for Emacs