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

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; -*-
;; sample use of abbrev

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

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

    ;; net abbrev
    ("afaik" "as far as i know" )
    ("atm" "at the moment" )
    ("dfb" "difference between" )
    ("ty" "thank you" )
    ("ui" "user interface" )
    ("uns" "understand" )
    ("ur" "you are" )
    ("btw" "by the way" )

    ("cnt" "can't" )
    ("ddnt" "didn't" )
    ("dnt" "don't" )

    ;; english word abbrev
    ("ann" "announcement" )
    ("arg" "argument" )
    ("autom" "automatic" )
    ("bc" "because" )
    ("bg" "background" )
    ("bt" "between" )
    ("math" "mathematics" )

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

    ;; tech company
    ("gc" "Google Chrome" )
    ("gm" "Google Map" )
    ("macos" "Mac OS" )
    ("msw" "Microsoft Windows" )

    ;; programing
    ("ev" "environment variable" )
    ("ipa" "IP address" )
    ("jvm" "Java Virtual Machine" )
    ("rsi" "Repetitive Strain Injury" )
    ("subdir" "sub-directory" )
    ("wd" "web development" )

    ("db" "database" )
    ("gui3" "graphical user interface" )
    ("oop3" "object oriented programing" )

    ("os3" "operating system" )

    ;; programing
    ("eq" "==" )
    ("r" "return" )
    ("utf8" "-*- coding: utf-8 -*-" )

    ;; regex
    ("xaz" "\\([A-Za-z0-9]+\\)" )

    ;; unicode
    ("md" "—" )

    ("bu" "•" )
    ("catface" "😸" )
    ("hearts" "♥💕💓💔💖💗💘💝💞💟💙💚💛💜" )
    ("ra" "→" )

    ;; url
    ("urlemacs" "http://ergoemacs.org/" )

    ;;
    ))

;; define abbrev for specific major mode
;; the first part of the name should be the value of the variable major-mode of that mode
;; e.g. for go-mode, name should be go-mode-abbrev-table
(define-abbrev-table 'go-mode-abbrev-table
  '(
    ("g3" "package main
import \"fmt\"
func main() {
        fmt.Println(\"3\")
}")

    ("for" "for i := 0; i < 4; i++ { i }")
    ("if" "if x < 0 { 3 }")
    ("r" "return")
    ("ps" "+")
    ("eq" "==")
    ("pt" "fmt.Println(3)")
    ("fu" "func(x int) int { return 1 }")
    ("v" "var = 3")

    ;;

    ))

(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")

Select the line, then Alt+x eval-region

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

Edit Abbrev

To edit the abbrev, just open the file, add/remove a line, then Alt+x 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 word can contain letters and digits, and can start with a digit.

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

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

Liket it? Put $1 at patreon. Or Buy Xah Emacs Tutorial. Thanks.