Emacs: Setup Hundreds of Abbrevs
Keeping Hundreds Abbrevs in Order and Grouping
This page shows you how to organize hundreds of abbrevs of Emacs: Abbrev Mode, by using elisp code directly.
The advantage of this is that you can keep the order of your abbrevs, and group them into categories, e.g. English word abbrevs, function template abbrevs, shell command abbrevs, personal abbrevs (company names, phone, url etc), math symbols, emoji, etc.
Create Abbrevs
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 '( ;; English word abbrev ("arg" "argument" ) ("bc" "because" ) ("bg" "background" ) ("bt" "between" ) ;; English abridged words ("cnt" "can't" ) ("ddnt" "didn't" ) ("dnt" "don't" ) ;; phrase abbrev ("afaik" "as far as i know" ) ("atm" "at the moment" ) ("ty" "thank you" ) ("btw" "by the way" ) ;; computing ("cfg" "context-free grammar" ) ("cs" "computer science" ) ;; tech company ("gc" "Google Chrome" ) ("macos" "macOS" ) ("msw" "Microsoft Windows" ) ;; programing ("ipa" "IP address" ) ("jvm" "Java Virtual Machine" ) ("rsi" "Repetitive Strain Injury" ) ("subdir" "subdirectory" ) ("db" "database" ) ("evp" "environment variable" ) ("guip" "graphical user interface" ) ("oopp" "object oriented programing" ) ("osp" "operating system" ) ;; programing ("eq" "==" ) ("r" "return" ) ("utf8" "-*- coding: utf-8 -*-" ) ;; regex ("rgaz" "\\([A-Za-z0-9]+\\)") ("rgshy" "\\(?:xx\\)") ("rgbracket" "\\[\\([^]]+?\\)\\]") ("rgcurly" "“\\([^”]+?\\)”") ("rgdigits" "\\([0-9]+\\)") ("rgdate" "\\([0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]\\)") ("rgdot" "\\(.\\)") ("rgstr" "\\([^\"]+?\\)") ("rgtag" "\\([</>=\" A-Za-z0-9]+\\)") ;; unicode ("bu" "•" ) ("catface" "😸" ) ("hearts" "♥💓💕💙💚💛💜" ) ("ra" "→" ) ;; )) ;; 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 (progn (when (boundp 'go-mode-abbrev-table) (clear-abbrev-table go-mode-abbrev-table)) (define-abbrev-table 'go-mode-abbrev-table '(("go" "package main import \"fmt\" func main() { fmt.Println(\"3\") }") ("p" "fmt.Printf(\"%v\\n\", hh▮)") ("pl" "fmt.Println(hh▮)") ("r" "return") ("st" "string") ("eq" "==") ("v" "var x = 3") ("df" "x := 3") ("c" "const x = 3") ("f" "func ff(x int) int { return nil }") ("if" "if 4 { 3 }") ("ie" " if err != nil { panic(err) }") ("ei" "else if x > 0 { 3 }") ("else" "else { 3 }") ("for" "for i := 0; i < 4; i++ { i }") ("fr" "for k, v := range xxx { ▮ } ")))) (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.
put this in your Emacs Init File:
(load "~/.emacs.d/lisp/my-abbrev.el")
Now, when you type 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 Emacs: Bookmark , so it's fast to open.
Prevent Abbrev Expansion
To stop abbrev from expansion, press Ctrl+q before typing space or punctuation.
Allowed Abbrev Characters
- Abbrev word can contain letters and digits, and can start with a digit.
- Abbrev cannot contain punctuation.