Note: This article is part of my Emacs for Writers series.
Emacs gives the user a deeper level of control and power over their writing output. So you can get more done faster. Often, it’s not the big enhancements that give the greatest freedom, but the little ones, like text expansion and a small nicety, that can greatly improve your speed and typing accuracy. But whether you type fast or slow, you can benefit from easy text expansion. Here’s the breakdown of what we’ll cover in this article:
Table of Contents
The magic of abbrev mode
“Abbrev” is a minor mode in Emacs that takes an abbreviation and expands it into full text.
For example, I can’t remember how to spell Zettelkasten, but I use the word often enough that I set a “Zt” as an abbrev for the full word. This means whenever I type “Zt,” Emacs will expand that snippet to the full word, Zettelkasten.
You can also use abbrev mode to automatically catch common mistakes in your writing. For example, due to typing too fast, I often type “teh” instead of the good and proper “the.” In effect, this means “the” is the abbreviation, and “the” is its expansion.
Important. Note that abbrev mode, like other minor modes in Emacs, is not enabled by default. You will need to either turn it on in your session like so:
M-x abbrev-mode
Or enable it at startup via your config file. I have it hooked up like this in my config so that abbrev mode is enabled whenever I visit a text mode buffer, which includes Org Mode buffers:
(add-hook 'text-mode-hook (lambda () (visual-line-mode) ;; enables a mode that wraps text in the buffer (abbrev-mode) ;; enables abbrev mode ))
Or, you can set it standing on its own line, like this:
(setq-default abbrev-mode t)
However you choose to enable it, just make sure it’s set.
Create your own abbrev defintions
First, you will need to decide if the expansion you want to create should be saved in the global abbrev table (usable everywhere in Emacs) or into the local table based on your current mode (only available in the current mode, Org Mode, for example). For most of your everyday writing needs, I’d suggest starting with the local mode because there may be instances in which you are doing something out of your normal context, like Emacs Lisp coding, for example, in which your customary will be useless or, worse, a disruption. It’s up to you.
Setting abbrevs with key commands
To add a global abbrev, you can use this command: C-x a g
. This will grab the nearest word behind your cursor and use it as the expansion, prompting you to provide an abbrev. That might not be what you want. If you want to use an entire phrase as an expansion, like expand “ttyl” into “talk to you later,” for example, you will highlight the whole phrase, “talk to you later,” and then key in the command.
For a local abbrev, which takes your current mode as the argument, just substitute the “g” in the keybinding for an “l” (for local): C-x a l
.
With Emacs Lisp statements
In this section, you’ll have a chance to learn about evaluating Emacs Lisp expressions. The great part is that these can be
Setting a global abbrev with Emacs Lisp can be done as follows. Copy this text into an Org Mode buffer and press C-c c
on it. This command will do some Org Mode magic and ask you if you want to evaluate the code block. Press “y” for yes.
#+begin_src elisp (define-abbrev global-abbrev-table "ttyl" "talk to you later") #+end_src
Likewise, you could set an abbreviation just for text modes, which would include Org Mode as follows.
#+begin_src elisp (define-abbrev text-mode-abbrev-table "ttyl" "talk to you later") #+end_src
Once you evaluate this statement, the abbrev will be available right away.
Saving your abbrevs into memory
As I mentioned in the very first article in the series, Emacs is a “living environment,” so in order for the program to remember your abbrevs, Emacs will save them to a file that will read upon startup whenever you open the program.
By default, Emacs uses a file in your home directory called .abbrev_defs
.
Upon closing Emacs, you will be prompted to save any new abbrevs permanently in your list of abbrev definitions. Answer yes to save it.
Unexpand an abbrev
There may be occasions in which you do not want an abbrev to expand.
That’s it! Well done on completing this tutorial.