Using Emacs Abbrev Mode for Abbreviation

Buy Xah Emacs Tutorial. Master emacs benefits for life.
, , …,

This page is a tutorial on how to use emacs's abbrev feature.

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

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

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

;; 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 a line.

You may want to add your abbrev file to Emacs's Bookmark, so you can open fast. (more advanced is to use Emacs: Hotkey to Open File Fast.)

Allowed Abbrev Chars

abbrev only allow sequence of English letters to be used as input. So, you cannot define -> as abbrev for . You might use the abbrev “rarr”.

For lots math symbols, see: Emacs: xah-math-input.el.

Emacs's Traditional Method for Using the Abbrev System

Emacs has about 14 commands for {adding, removing, listing, editing} abbrevs. You can read about it at: (info "(emacs) Abbrevs").

Like it? Buy Xah Emacs Tutorial.
blog comments powered by Disqus