Yankpad

Text snippets can speed writing code quite a bit, but, somehow, I never managed to get used to the solutions Emacs provides, such as Yasnippet and Skeleton. As a consequence, I seldom, if at all, use text snippets.

A few days ago I came across Yankpad, an Emacs lisp package for managing text snippets and (can you guess?) I might have finally found the solution I was looking for! The package is available on MELPA.

Yankpad is based on OrgMode: snippets are defined in a OrgMode file (yankpad.org, by default), in which Level 1 headings are used to group snippets and Level 2 headings are used to define a snippet. In this way all snippets are defined in a single, nicely structured, file1.

First level headers of the yankpad can be associated to major modes, in which case the corresponding snippets are made available in buffers visited in the major mode; alternatively all snippets belonging to a level-1 heading can be enabled manually by invoking yankpad-set-category.

If used in conjunction with Yasnippet, snippets can use the syntax and expansion capabilities provided by this package, which make snippets defined with Yankpad much more useful.

For instance the following portion of my yankpad defines a snippet Cash Withdrawal, which is available in any buffer in hledger-mode. The snippet uses Emacs-lisp to insert the current date (formatted as YYYY-MM-DD, e.g., 2018-10-20) and sets two tab stops ($1 and $0):

* hledger-mode
** cw: Cash Withdrawal                                                  :src:

   #+BEGIN_SRC 
   `(format-time-string "%Y-%2m-%2d" (current-time))`  Me
     cash     $1
     bank

     $0
   #+END_SRC

The example shows two other nice features of Yankpad, namely, shortcuts and literate-programming-styled snippets. In fact, the cw string at the beginning of the heading allows to invoke snippet expansion by writing cw in the buffer and then call yankpad-expand. The :src: tag specifies that the snippet is defined in a source code drawer.

Make sure you globally enable yas-mode, if you intend to use Yankpad with Yasnippet.

This, for instance, is my setup:

(require 'yasnippet nil t)
(if (featurep 'yasnippet)
    (yas-global-mode 1))

(require 'yankpad nil t)
(global-set-key "\C-x?" 'yankpad-insert)
(global-set-key "\C-x/" 'yankpad-expand)

Have a look at the documentation and the repository for more details.

A side-effect of using an OrgMode file to define snippets is that they can be easily published. Publication in Jekyll requires a bit of preparation. More in details I had to:

  1. Install the [org-mode converter][] in the _plugins directory
  2. Write a small script which takes my yankpad, copies it in the sources of my site (to make it available for download) and creates another file in which YAML front-matter is prepended to the yankpad (so that Jekyll recognizes and processes the file).

The result is available here and this is the script:

# make a literal copy of the file
cp ~/OrgMode/yankpad.org ~/Sites/home/assets/2018-10-28-yankpad

# create a page recognized by Jekyll with the Yankpad
cat > ~/Sites/home/assets/2018-10-28-yankpad/yankpad-rendered.org <<EOF
---
title: Yankpad
layout: page
---
[[./yankpad.org][RAW File]]
EOF
cat ~/Sites/home/assets/2018-10-28-yankpad/yankpad.org >> ~/Sites/home/assets/2018-10-28-yankpad/yankpad-rendered.org
  1. Yankpad also defines features to separate the definition of snippets in different files. 

Get in touch

Comments