Yankpad

Menu

Note: the original content of this post has been edited on <2020-06-04 Thu>.

Text snippets can speed writing code quite a bit and, over time, I started appreciating and using Yasnippet.

A few days ago I came across Yankpad, an Emacs lisp package for managing text snippets which is quite interesting. 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. 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.

If you intend to use Yankpad with Yasnippet make sure you enable yas-mode globally. This is, for instance, a possible 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 Org Mode file to define snippets is that your snippets can be more easily published by any tool supporting the mode.

Footnotes:

1

Yankpad also defines features to separate the definition of snippets in different files.

Author: Adolfo Villafiorita

Last modified: 2020-06-29 Mon 22:41 (created on: 2018-10-28 Sun 00:00)

Published: 2020-06-30 Tue 22:55