Using Emacs's Abbrev Mode for Abbreviation

Perm url with updates: http://xahlee.org/emacs/emacs_abbrev_mode.html

Using Emacs's Abbrev Mode for Abbreviation

Xah Lee, 2010-09-19

This page is a short tutorial on how to use emacs's abbrev mode for abbreviation.

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
    ("tin" "∈" nil 0)
    ("tnin" "∉" nil 0)
    ("tinf" "∞" nil 0)
    ("tluv" "♥" nil 0)
    ("tsmly" "☺" nil 0)

    ;; email
    ("twdy" "wordy-english@yahoogroups.com" nil 0)

    ;; computing tech
    ("twp" "Wikipedia" nil 0)
    ("tms" "Microsoft" nil 0)
    ("tg" "Google" nil 0)
    ("tqt" "QuickTime" nil 0)
    ("tit" "IntelliType" nil 0)
    ("tmsw" "Microsoft Windows" nil 0)
    ("twin" "Windows" nil 0)
    ("tie" "Internet Explorer" nil 0)
    ("tahk" "AutoHotkey" nil 0)
    ("tpr" "POV-Ray" nil 0)
    ("tps" "PowerShell" nil 0)
    ("tmma" "Mathematica" nil 0)
    ("tjs" "javascript" nil 0)
    ("tvb" "Visual Basic" nil 0)
    ("tyt" "YouTube" nil 0)
    ("tge" "Google Earth" nil 0)
    ("tff" "FireFox" nil 0)
    ("tsl" "Second Life" nil 0)
    ("tll" "Linden Labs" nil 0)
    ("tcs" "Chthonic Syndicate" nil 0)
    ("tee" "ErgoEmacs" nil 0)

    ;; normal english words
    ("talt" "alternative" nil 0)
    ("tchar" "character" nil 0)
    ("tdef" "definition" nil 0)
    ("tbg" "background" nil 0)
    ("tkb" "keyboard" nil 0)
    ("tex" "example" nil 0)
    ("tkbd" "keybinding" nil 0)
    ("tenv" "environment" nil 0)
    ("tvar" "variable" nil 0)
    ("tev" "environment variable" nil 0)
    ("tcp" "computer" nil 0)

    ;; sig
    ("txl" "Xah Lee" nil 0)
    ("txs" " Xah ∑ xahlee.org ☄" nil 0)

    ;; url
    ("tuxl" "http://xahlee.org/" nil 0)
    ("tuxp" "http://xahporn.org/" nil 0)
    ("tuee" "http://ergoemacs.org/" nil 0)
    ("tuvmm" "http://VirtualMathMuseum.org/" nil 0)
    ("tu3dxm" "http://3D-XplorMath.org/" nil 0)

    ;; emacs regex
    ("tnum" "\\([0-9]+?\\)" nil 0)
    ("tstr" "\\([^\"]+?\\)\"" nil 0)
    ("tcurly" "“\\([^”]+?\\)”" nil 0)

    ;; shell commands
    ("tditto" "ditto -ck --sequesterRsrc --keepParent src dest" nil 0)
    ("tim" "convert -quality 85% " nil 0)
    ("tims" "convert -size  -quality 85% " nil 0)
    ("tim256" "convert +dither -colors 256 " nil 0)
    ("timf" "find . -name \"*png\" | xargs -l -i basename \"{}\" \".png\" | xargs -l -i  convert -quality 85% \"{}.png\" \"{}.jpg\"" nil 0)

    ("t0" "find . -type f -empty" nil 0)
    ("t00" "find . -type f -size 0 -exec rm {} ';'" nil 0)
    ("tchmod" "find . -type f -exec chmod 644 {} ';'" nil 0)
    ("tchmod2" "find . -type d -exec chmod 755 {} ';'" nil 0)

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

    ("trsync2" "rsync -r -v -t --delete --rsh=\"ssh -l xah\" ~/web/ xah@example.com:~/web/" nil 0)
    ("trsync3" "rsync -r -v -t --delete --exclude=\"**/My *\" --rsh=\"ssh -l xah\" ~/Documents/ xah@example.com:~/Documents/" nil 0)
    ))

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

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 “t0” then press Space, then it'll expand to:

find . -type f -empty

I add the letter “t” in front of all my abbrevs. For example, my abbrev for “character” is “tchar” not “char”. This way, it is easier for me to avoid expanding when i don't want to.

The “t” is right under the right hand's index finger on The Dvorak Keyboard Layout. If you are using QWERTY, you can use “k”.

Uses for Abbreviation

You can use abbrev for inserting Math Symbols or other Unicode Characters (e.g. “∈ → ⇒ ∞ π”), or normal long English words (e.g. “alternative”, “organization”, “PowerShell”, “Wikipedia”), or your email address, signature, urls you need often, and most useful to me is long unix shell commands. For example, i type “trsync” and it expands to: 「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 104454842" ~/web/ 0 104454842@s368352655.example.com:~/ 」.

You can also use it to insert programing language's function templates, or xml templates, but a more powerful feature for that is Emacs Templates with YASnippet.

Adding and Removing Your Abbrevs

You may want to put your abbrev file in Emacs's Bookmark, so you can easily open it.

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

Emacs's Way

Emacs has many shortcuts and over ten commands for adding, removing, listing, editing abbrevs. You can read the manual at: (info "(emacs) Abbrevs").

For example, to add a new abbrev, 【Ctrl+x a g】 will promp you to enter a abbrev for the word in front of 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〕.

I didn't find the many extra add/remove/prevent/“mode specific” abbrev features useful. But if you like to use emacs's abbrev shortcuts and commands, just remember its file location.

If you always add new abbrevs to the definition file yourself, then you may add the following code in your emacs init file:

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

(info "(elisp) Abbrevs")

Was this page useful? If so, please do donate $3, thank you donors!

Popular posts from this blog

11 Years of Writing About Emacs

does md5 creates more randomness?

Google Code shutting down, future of ErgoEmacs