Protesilaos Stavrou (Prot) has posted a nifty video on abbrev-mode. It turns out that there’s a lot more to it than you might have thought. Prot says that you can think of it as a lightweight alternative for yasnippet or something similar. My view is that if you’re already familiar with YASnippet you should just use that but if you aren’t familiar with it or just want something simpler, abbrev-mode may be just what you need.
As Prot shows, it’s pretty easy to set up a simple abbreviation: you simply call the define-abbrev
function specifying the abbreviation and its expansion. But even in this simple case there are some nuances. You also have to specify the major mode the abbreviation will be active in. That can be global or it can be more specific. The modes form a hierarchy with global mode at the top, more specific modes (such as text-mode) next, and then derived modes (such as org-mode). Abbreviations for lower modes override the same abbreviation at higher levels so there’s a consistent inheritance mechanism.
That’s all there is to the simple case but Prot considers two more complicated cases. First, you can replace the expansion string with a function that will return the expansion value. Prot uses the example of a function that returns an ASCII representation of the current time.
Next, he considers the case of expanding something like :emojiX
to the corresponding emoji. The problem here is that abbrev-mode will not recognize :emojiX
as a word because of the colon. You can tell abbrev-mode to recognize such things by adjusting the regular expression it uses to recognize abbreviations.
Prot gives all the details in his video and there’s a copy of his scratch file at the link so you don’t have to worry about copying the things he does down. The video is 26 minutes, 22 seconds long so you’ll have to schedule some time. As usual with Prot’s videos, it’s a worthwhile investment.