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
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 (
* 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,
cw string at the beginning of the heading allows to invoke
snippet expansion by writing
cw in the buffer and then call
:src: tag specifies that the snippet is
defined in a source code drawer.
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)
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:
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
Yankpad also defines features to separate the definition of snippets in different files. ↩
Get in touch