ErgoEmacsEmacsLispBlogEmacsLispBuy Tutorial
Web Hosting by 1&1

Using Emacs Abbrev Mode for Abbreviation

Xah Lee, , …,

This page is a short tutorial on how to use emacs's abbrev mode for typing long words (⁖ “i18n” for “Internationalization”), special characters or math symbols (⁖ lambda for λ, heart for ♥), or long shell commands.

Emacs has a abbrev feature that's really useful, and is usually not known to beginners. I lived in emacs daily in a programing day job since 1998, but only learned and started to use abbrev mode in about 2007. Close to 10 years i didn't use abbrev feature, and was too lazy to read about it.

Defining Your Abbrevs

Create a file with the following content:

; my personal abbreviations
(define-abbrev-table 'global-abbrev-table '(

    ;; math/unicode symbols
    ("8in" "∈")
    ("8nin" "∉")
    ("8inf" "∞")
    ("8luv" "♥")
    ("8smly" "☺")

    ;; email
    ("8wdy" "wordy-english@yahoogroups.com")

    ;; computing tech
    ("8wp" "Wikipedia")
    ("8ms" "Microsoft")
    ("8g" "Google")
    ("8qt" "QuickTime")
    ("8it" "IntelliType")
    ("8msw" "Microsoft Windows")
    ("8win" "Windows")
    ("8ie" "Internet Explorer")
    ("8ahk" "AutoHotkey")
    ("8pr" "POV-Ray")
    ("8ps" "PowerShell")
    ("8mma" "Mathematica")
    ("8js" "javascript")
    ("8vb" "Visual Basic")
    ("8yt" "YouTube")
    ("8ge" "Google Earth")
    ("8ff" "Firefox")
    ("8sl" "Second Life")
    ("8ll" "Linden Labs")
    ("8ee" "ErgoEmacs")

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

    ;; sig
    ("8xl" "Xah Lee")
    ("8xs" " Xah ∑ xahlee.org ☄")

    ;; url
    ("8uxl" "http://xahlee.org/")
    ("8uxp" "http://xahporn.org/")
    ("8uee" "http://ergoemacs.org/")
    ("8uvmm" "http://VirtualMathMuseum.org/")
    ("8u3dxm" "http://3D-XplorMath.org/")

    ;; emacs regex
    ("8num" "\\([0-9]+?\\)")
    ("8str" "\\([^\"]+?\\)\"")
    ("8curly" "“\\([^”]+?\\)”")

    ;; shell commands
    ("8ditto" "ditto -ck --sequesterRsrc --keepParent src dest")
    ("8im" "convert -quality 85% ")
    ("8ims" "convert -size  -quality 85% ")
    ("8im256" "convert +dither -colors 256 ")
    ("8imf" "find . -name \"*png\" | xargs -l -i basename \"{}\" \".png\" | xargs -l -i  convert -quality 85% \"{}.png\" \"{}.jpg\"")

    ("8f0" "find . -type f -empty")
    ("8f00" "find . -type f -size 0 -exec rm {} ';'")
    ("8chmod" "find . -type f -exec chmod 644 {} ';'")
    ("8chmod2" "find . -type d -exec chmod 755 {} ';'")

    ("8unison" "unison -servercmd /usr/bin/unison c:/Users/xah/web ssh://xah@example.com//Users/xah/web")
    ("8sftp" "sftp xah@xahlee.org")
    ("8ssh" "ssh xah@xahlee.org")
    ("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:~/")

    ("8rsync2" "rsync -r -v -t --delete --rsh=\"ssh -l xah\" ~/web/ xah@example.com:~/web/")
    ("8rsync3" "rsync -r -v -t --delete --exclude=\"**/My *\" --rsh=\"ssh -l xah\" ~/Documents/ xah@example.com:~/Documents/")
    ))

;; 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. For example, my abbrev for the word “variable” is “8var”. This way, it is easier for me to avoid expanding when i 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. You could use another rare starting letter, such as “z”.

What Are Some Uses of Emacs Abbreviation System?

You can use abbrev for:

Adding and Removing Your Abbrevs

You may want to put your abbrev file in Emacs's Bookmark, so you can easily open it for addition. Or, you can make a single button to open it, like this:

; open my abbrev file with F8 key
(global-set-key (kbd "<f8>") (lambda () (interactive) (find-file "~/.emacs.d/my_emacs_abbrev.el")))

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

Emacs's Traditional Method for Using the Abbrev System

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

For example, to add a new abbrev, 【Ctrl+x a g】 will prompt you to enter a abbrev for the word before the cursor.

Emacs does not automatically save your abbrev, but will ask you when you quit emacs. By default the abbrev file is saved at: 〔~/.emacs.d/abbrev_defs〕.

For me, i do not use emacs's interface to abbrev, because i find it easier to add/delete/list/read abbrevs by manually editing my abbrev file. Emac's keys are hard to remember and not efficient. It has keys to expand abbrev, prevent expansion, add into the current mode, etc. The features are too elaborate that i didn't find a need. Emacs's interface might be nice when you start to have hundreds of abbrevs for tens of major modes.

blog comments powered by Disqus