All Projects → abo-abo → Auto Yasnippet

abo-abo / Auto Yasnippet

quickly create disposable yasnippets

Auto-YASnippet

This is a hybrid of keyboard macros and yasnippet. You create the snippet on the go, usually to be used just in the one place. It's fast, because you're not leaving the current buffer, and all you do is enter the code you'd enter anyway, just placing ~ where you'd like yasnippet fields and mirrors to be.

Table of Contents

Installation instructions

It's easiest/recommended to install from MELPA. Here's a minimal MELPA configuration for your ~/.emacs:

(package-initialize)
(add-to-list 'package-archives '("melpa" . "http://melpa.org/packages/"))

Afterwards, M-x package-install RET auto-yasnippet RET (you might want to M-x package-refresh-contents RET beforehand if you haven't done so recently).

You will also want to setup the key bindings. Here's what I recommend:

(global-set-key (kbd "H-w") #'aya-create)
(global-set-key (kbd "H-y") #'aya-expand)

I also like to bind this, instead of using TAB to expand yasnippets:

(global-set-key (kbd "C-o") #'aya-open-line)

Usage

A basic example

Suppose we want to write:

count_of_red = get_total("red");
count_of_blue = get_total("blue");
count_of_green = get_total("green");

We write a template, using ~ to represent variables that we want to replace:

count_of_~red = get_total("~red");

Call aya-create with point on this line, and the template is converted to a value we want:

count_of_red = get_total("red");

Then call aya-expand and you can 'paste' additional instances of the template. Yasnippet is active, so you can tab between placeholders as usual.

count_of_red = get_total("red");
count_of_ = get_total("");

Inline text

~ replaces the symbol after it. If you want to replace arbitrary text, use Emacs-style backticks:

`red'_total = get_total("`red'_values");

Multiple placeholders

You can replace multiple values in a template, just like normal yasnippet.

In this example, our template has multiple lines, so we need to select the relevant lines before calling aya-create.

~FooType get~Foo() {
    // Get the ~foo attribute on this.
    return this.~foo;
}

We only fill in three placeholders in this example (the fourth is the same as the third).

JavaScript - aya-create-one-line:

aya-create-one-line works as a combination of aya-create and aya-expand for one-line snippets. It's invoked by aya-create in case there's no aya-marker (default ~) on the line, but there's aya-marker-one-line (default $). Or you can invoke it on its own.

field$ = document.getElementById("");

call aya-create and the rest is as before:

field1 = document.getElementById("field1");
field2 = document.getElementById("field2");
field3 = document.getElementById("field3");
fieldFinal = document.getElementById("fieldFinal");

Generating comments

Here's a yasnippet that makes use of aya-tab-position. You need to call aya-open-line if you want to use it.

# -*- mode: snippet -*-
# name: short comment
# key: sc
# --
//———$1${1:$(make-string (- 47 aya-tab-position (length yas-text)) ?—)}$0

Comments generated with this will always end in same column position, no matter from which indentation level they were invoked from.

Mixed case templates

You can create mixed case templates setting aya-case-fold to t. This will result in templates where variables that start with a character of a different case will be treated as the same variable. The case of the first character will be preserved in the resulting snippet.

Using the earlier example with a slight twist:

count_of_~red = get_total("~Red");

Then calling aya-create, then aya-expand, and finally typing blue, the result would be:

count_of_blue = get_total("Blue");

Notice that blue was placed in both locations with proper casing.

Functions

aya-create

Removes "~" from current line or region (if mark is active) yielding valid code. The created snippet is recorded into aya-current.

aya-expand

Expands whatever is currently in aya-current

aya-open-line

Generic expansion function. It will either expand or move to the next field depending on the context.

aya-persist-snippet

Save the current auto-snippet to a user snippets folder (this defaults to ~/.emacs.d/snippets/.) The current major-mode name will be used to determine the snippets sub-directory to store the snippet. For example when working in js2-mode the snippet will be saved to (by default) ~/.emacs.d/snippets/js2-mode/.

You will be prompted for the snippet name. The appropriate file will be opened but not saved, with the point on the key: parameter of the snippet. If you wish to proceed, fill in the key, save the buffer and call C-c C-l (yas-load-snippet-buffer). Otherwise, simply kill the buffer - there will be no side effects.

You can customize aya-persist-snippets-dir to use a different folder for storing auto-snippets.

You will need to run yas/reload-all before using the new snippet with its key trigger.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].