yankpad.el 
Let’s say that you have text snippets that you want to paste, but that yasnippet or skeleton is a bit too much when you do not need a shortcut/abbrev for your snippet. You like org-mode, so why not write your snippets there? Introducing the yankpad:
* Category 1
** Snippet title
Here's a text snippet I want to insert.
** Snippet with keybinding :last:tag:is:key:o:
And here's another snippet. This snippet has tags, and the last of these
tags should be a key. This will bind the snippet to the key (in this case
"o") when first calling yankpad-map.
** expandword: Snippet with keyword expansion
This snippet has a keyword; "expandword" in this case. If this category is
active, and you type the keyword into a buffer and use the "yankpad-expand"
command, the keyword will be replaced with this snippet.
* Category 2
** Explaining categories
This snippet belongs to another category. Categories are useful if you need
several yankpads, for instance if you're a teacher (like me) working with
different courses.
** yasnippet magic
If you have yasnippet installed (not a requirement), the content in each snippet
is actually executed by yasnippet! This means that you could run elisp
inside your snippets: `(+ 3 4)` and have handy tab stop fields.
| Student | Grade |
|---------+-------|
| $1 | $2 |
That's pretty handy!
$0
** The source block below will be executed if tag is func :func:
#+BEGIN_SRC emacs-lisp
;; Instead of a src-block, the snippet may be named
;; the same as an emacs-lisp function. This will then
;; be executed without arguments (see next example).
(elfeed)
#+END_SRC
** elfeed :func:e:
* Kitchen sink category
:PROPERTIES:
:INCLUDE: Category 1|Category 2
:END:
** Include other categories
Snippets from Category 1 and Category 2 will be appended to this category.
This is done by setting the INCLUDE property of the category. Categories
are separated by a pipe.
* org-mode
** Major-mode categories
If you have a category with the same name as a major-mode, that category will be
activated when switching major-mode. This only affects the local buffer and does
not modify the global category.
* my-projectile-project
** Projectile based categories
If you have projectile installed (not a requirement) you can give a category the
same name as one of your projectile projects. That category will be activated
when using projectile-find-file on a file in the project.
Setup
- Install
yankpadfrom Melpa, or downloadyankpad.eland add it to your load-path and require it. - The default location for the yankpad file is
yankpad.orgin yourorg-directory. This can be changed by modifying theyankpad-filevariable. - Optionally bind
yankpad-map,yankpad-insert, and/oryankpad-expandto a key. - Optionally install
yasnippetand/orprojectileand/orcompany-mode, if you want the additional yankpad features that those package provide. - That’s it!
If you want different heading levels for the categories (default 1) or snippets (default 2), change the value of yankpad-category-heading-level and/or yankpad-snippet-heading-level.
By default keywords are defined by a word at the start of a snippet title, followed by a colon (:). You can change : into another string by changing the yankpad-expand-separator variable.
Here’s an example setup using the excellent use-package:
(use-package yankpad
:ensure t
:defer 10
:init
(setq yankpad-file "~/yankpad.org")
:config
(bind-key "<f7>" 'yankpad-map)
(bind-key "<f12>" 'yankpad-expand)
;; If you want to complete snippets using company-mode
(add-to-list 'company-backends #'company-yankpad))Usage
- Add snippet entries to your
yankpad-file. Level 1 headings are categories and level 2 headings are snippets (by default). A quick way to open youryankpad-fileis to useM-x yankpad-edit. You can also add snippets to the currentyankpad-categoryby usingM-x yankpad-capture. - Insert a snippet with
M-x yankpad-insert. If the snippet has a keyword (it starts with a word followed by a colon), you can write that keyword into the buffer and useM-x yankpad-expandinstead. It may be useful to bind these commands to some key on your keyboard. You can also usecompany-yankpadto expand a snippet usingcompany-mode(thanks sid-kurias for contributing). If you want to insert the last snippet again, you can useM-x yankpad-repeat(bind that to a key if you’re using it frequently). - If you want to change category, use
M-x yankpad-set-category. If you have a category with the same name as a major-mode (for instanceorg-mode), that category will be locally set when switching major-mode. In the same manner you can name a category to one of your Projectile project names (if Projectile is installed). If both cases are true, the Projectile category becomes active, but the snippets from the major mode are appended as well. If you later change category withM-x yankpad-set-category, the major-mode and project snippets will be appended to the chosen category. - If you want to append snippets from another of your categories (basically like having two or more categories active at the same time), use
M-x yankpad-append-category. If you want one of your categories to always include snippets from another category; set theINCLUDEproperty of the category heading (several categories can be included this way, by separating them with|, see example at the top of this readme). - To quickly open your
yankpad-filefor editing, runM-x yankpad-edit. - Yankpad caches your snippets, making it a bit snappier to insert snippets from the yankpad. If you’ve edited your
yankpad-fileyou might want to useM-x yankpad-reloadto clear the snippet cache and reload your snippets in the current category.
Since a * at the beginning of a line would specify a new heading, lines can not begin with *. However, you can write \* at the beginning of a line, which will be replaced by a * when expanding the snippet. If you use this in order to yank snippets into an org-mode buffer, the new headings will be automatically indented – depending on the current level – by default. This can be changed by setting the variable yankpad-respect-current-org-level to nil.
Sometimes it may be useful to set the category automatically for a specific file. In this case you can add yankpad-category as a file variable, for instance by adding this line at the top of your file:
-*- yankpad-category: "Category name"; -*-
You can also set the yankpad-category to nil in this way, if you do not want any default category triggered for that file.
Special tags
Snippets in your Yankpad can have tags, and some of these have special meanings:
func- If a snippet has a tag named
func, it won’t insert text. Instead a function will be executed upon “inserting” the snippet. The name of the snippet can be an elisp function, which will be run without arguments. Instead, the function could hold a singleorg-modesrc-block, which will be executed in a separate buffer (so the code in the src-block does not have access to the current buffer). results- Works like
func, but the output of the function will be inserted into the buffer. indent_nil- By default the inserted text will be indented (uses
indent_regionor the settings ofyas-indent-lineifyas-minor-modeis active). By usingindent_nil, no indentation will occur. indent_auto- Sets
yas-indent-linetoautofor this snippet. indent_fixed- Sets
yas-indent-linetofixedfor this snippet. <key>- The last tag of a snippet (except if its one of the above) will add the tag as a keybinding when first calling
yankpad-map. If the last tag iso, then usingM-x yankpad-map owill insert that snippet. This is most useful if you bindyankpad-mapto a key.
Changelog
- 1.70 (February 2017)
yankpad-repeatandyankpad-capture-snippetadded.- 1.60 (January 2017)
company-yankpad(requires company-mode) was contributed by sid-kurias. You can now use company to complete snippet names!- 1.51 (January 2017)
- Added
yankpad-reload. - 1.50 (September 2016)
- It is now possible to have active snippets from several categories at once, by using
M-x yankpad-append-categoryor by modifying the yankpad file. This is done automatically for major mode and projectile categories. - 1.40 (August 2016)
- Added
resultstag. Works asfunctag, but the output of the function is inserted into the buffer. - 1.31 (August 2016)
- Snippets are indented as default. The indentation behaviour can be changed by using
indent_nil,indent_fixed, orindent_autoas tags for the snippet(s). - 1.30 (August 2016)
- Snippets can now have keywords. If typing the snippet keyword into the buffer, the snippet can be expanded by calling
yankpad-expand. Just name the snippetexpandword: Snippet nameand you can typeexpandword M-x yankpad-expandto insert it. - 1.20 (July 2016)
- Snippets can be used to execute functions, instead of inserting text. Add the tag
functo your snippet. The snippet can contain anorg-modesrc-block, which will be executed, or the snippet may be named the same as an emacs-lisp function, which will be executed without arguments. - 1.10 (May 2016)
- Snippets can have keybindings by tagging them. The last tag will be interpreted as a key and inserted into
yankpad-map.