Emacs: Abbrev Mode
What is Abbrev Mode
Emacs abbrev feature lets you type a short word and expand into a full word or code template.
For example:
u
→you
ra
→→
lol
→😂
rt
→return
fn
→function f () { return 3; }
findempty
→find . -depth -empty -type d
Turn on Abbrev Mode
Alt+x abbrev-mode
to turn it on. Call again to turn off.
Define Abbrev
Suppose you want to define “bg” → “background”.
- Type “background”.
- Alt+x
add-global-abbrev
, in the prompt, type “bg”.
Now, when you type “bg” followed by a space or return, it'll expand to “background”.
If the expanded text is more than one word, make a text selection first.
If you want the abbrev only for the current major mode,
- Alt+x
add-mode-abbrev
- Define abbrev for current Major Mode only.
- Alt+x
inverse-add-mode-abbrev
- Define global abbrev, but use current word as abbrev, and you type the expansion in prompt.
Remove Abbrev
To remove a definition,
give a negative argument
to
add-global-abbrev
or add-mode-abbrev
.
For example, to undefine the abbrev “bg”.
universal-argument
【Ctrl+u】, type “-1”.- Alt+x
add-global-abbrev
, in the prompt, type “bg”.
List Abbrevs
- Alt+x
list-abbrevs
- Display a list of defined abbrevs.
Edit Abbrevs
- Alt+x
edit-abbrevs
- Edit abbrev.
The number in the middle column is the number of times you've used (expanded) the abbrev.
- To remove a abbrev, just delete the line.
- To add a abbrev, just add a line.
When done, to load and or save, call any of:
edit-abbrevs-redefine
- Redefine abbrevs according to current buffer contents.
abbrev-edit-save-buffer
- Redefine and save to abbrev file.
abbrev-edit-save-to-file
- Redefine and save to abbrev file, but ask for new location.
Manual Load/Save Abbrev File
read-abbrev-file
-
Read abbrev definitions from file written with
write-abbrev-file
. write-abbrev-file
- Write all user-level abbrev definitions to a file.
Abbrev Example
You can use abbrev for:
- Long English words, e.g. “comm” for “communication”.
- Programing language function templates.
- emoji, such as “omg” for 😂
- math symbols such as “ra” for →
- Templates, such as license header.
- Address, url, telephone number, company name, etc.