The yasnippet project lets you insert snippets of code. A snippet is a template that can have substitutions replaced either manually or programmatically. The choice of which template to choose and expand is based on the buffer’s mode.

To begin, let’s try to configure YAS and insert static text. If you are using use-package, let’s have it installed and set up a personal directory for our templates (I’ll use snippet and template interchangeably):

( use-package yasnippet :ensure t :init ( yas-global-mode 1 ) :config ( add-to-list 'yas-snippet-dirs ( locate-user-emacs-file "snippets" )))

Let’s create a template by typing C-c & C-n … yeah, I never remember that either, so try: M-x yas-new-snippet (and with something like IDO, that can be just as fast).

You should now be presented with a new template buffer, and since it is using YAS, you can fill in the individual fields and use Tab to jump to the next field. Try making it look like:

# -*- mode: snippet -*- # name: blah # key: blah # -- Bling blargh-a bloo bloop!

Hit C-c C-c to install it. It will ask you what mode it should expect to expand it. At this point, hopefully you can just hit Return to accept the default.

Why yes, you normally will want to save it. Notice the default may be exactly where you’ll want to keep it too: ~/.emacs.d/snippets

Now, in a buffer of the same mode, type blah and hit Tab , and the text will be replaced with:

Bling blargh-a bloo bloop!

YAS comes with multiple ways of triggering a template, but let’s next talk about ways to make the results of the template more useful.