I started using emacs in 2020 after reading a long-lost article about using org-mode as a blogging platform. It hit at the right time as I was looking for a tool that enabled auto-generated, centralised, todo lists - something that was surprisingly difficult to do at the time.1
I have written quite a bit in the last five year, including a peer reviewed journal article and use Emacs every day in my corporate job, both during my science days but also since my move to IT.
I have learned loads but was quite sure I had a solid grasp of the basics. That was until today when I learned about the function M-x yas-expand
.
I have written about yas-snippet before. It is a great tool to pull in snippets of texts so speed up workflows. You can use it to bring in meeting note templates, or quickly create functions while writing code.
To create a yas-snippet, run M-x yas-new-snippet
. That will open a file with the below front matter
# -*- mode: snippet -*-
# name:
# key:
# --
Here you can name the snippet and, something we will return to later, set a key.
Let’s create a snippet that creates an org-mode todo:
# -*- mode: snippet -*-
# name: todo
# key: td
# --
* TODO ${1:Task Title}
${2:Description or notes}
Saving this and using M-x yas-insert-snippet
will allow you to select todo
and a simple template will be added to your org-mode buffer.2
Using ${1:Task Title}
you can set the first cursor position and typing anything will replace the default text. Pressing tab
will move you to ${2:Description or notes}
. Super cool allowing you to move quickly through your template.
The bit I’ve missed out is the key: td
part. This is what threw me today, learning about functionality in a tool I’ve been using for years. This allows you to type td
in a buffer, run M-x yas-expand
while on those letters and Emacs will insert the snippet for you.
I. Was. In. Shock.
I had assumed that the “key” would allow you to quickly select your needed snippet after using yas-snippet-insert
but it never seemed to work. Furthermore, I had no idea this functionality was baked in nor that it would be so useful.
Binding yas-expand
to, say C-c y
would turn a multi-step process into something super fast.
This is like the painting of the Forth Bridge. Once I thought I had learned something and had internalised the workflow, going back to the start and checking my assumptions has created a whole wealth of new possibilities. I know Emacs is like this, never finishing but constantly finding new paths to travel on.
Have you had this, or do you have some functionality you know is under used? Drop in a comment below.