• Stars
    star
    243
  • Rank 166,366 (Top 4 %)
  • Language
    Emacs Lisp
  • Created over 11 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

quickly create disposable yasnippets

Auto-YASnippet Tests

Auto-YASnippet 1.0.0

Auto-YASnippet is a hybrid of keyboard macros and YASnippet. You create the snippet on the go and it'll be ready to use immediately. Because you're not leaving the current buffer the workflow is very fast.

All you do is enter the code you'd enter anyway but placing ~ chars where you`d like YASnippet fields and mirrors to be.

Table of Contents

Setup/Install

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).

Configuration

In your Emacs init file set keys for the aya commands.

For example:

(global-set-key (kbd "C-c C-y w")   #'aya-create)
(global-set-key (kbd "C-c C-y TAB") #'aya-expand)
(global-set-key (kbd "C-c C-y SPC") #'aya-expand-from-history)
(global-set-key (kbd "C-c C-y d")   #'aya-delete-from-history)
(global-set-key (kbd "C-c C-y c")   #'aya-clear-history)
(global-set-key (kbd "C-c C-y n")   #'aya-next-in-history)
(global-set-key (kbd "C-c C-y p")   #'aya-previous-in-history)
(global-set-key (kbd "C-c C-y s")   #'aya-persist-snippet)
(global-set-key (kbd "C-c C-y o")   #'aya-open-line)

Examples

If we need to write some repetitive code in an expression:

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

We can write a template, using ~ to represent text we want to replace:

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

With the cursor on this line, or with selected text, call M-x aya-create RET. An auto-snippet is created and the text is converted to remove the ~ marker:

count_of_red = get_total("red");

Now we can call aya-expand and we can insert text at each marker in the template, note, because both words are the same, we just type it one.

Yasnippet controls all the interaction while expanding so refer to the yasnippet docs. Basic interaction is to enter text at a marker and TAB to the next one.

count_of_[CURSOR] = get_total("");

Say we enter blue once at `count_of_`` the result will be.

count_of_blue = get_total("blue");

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 fill in two placeholders in this example (the 2nd and 4th are the same as the 3rd). Yasnippet places us at 1st, entering: Type TAB (yasnippet puts us at 3rd) entering: bar TAB will expand to:

Type getBar() {
  // Get the bar attribute on this.
  return this.foo;
}

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.

Expanding around a region

If you create an auto-yasnippet with one field, it's value will be filled in from the current region. For example if we create a new snippet:

print("\(~thing)")

Select text:

myVar + 10

aya-expand gives us...

print("\(myVar + 10)")

You can also use the YASnippet built in marker $0 in the point where you want to finish expanding the snippet. YASnippet allows $0 to be the region, by setting yas-wrap-around-region to t.

Functions

aya-create

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

aya-expand

Expands whatever is currently in aya-current

aya-expand-from-history

Select and expand from snippets in aya-history. The selected snippet will become aya-current.

aya-delete-from-history

Select and delete a snippet from aya-history. The next available snippet will become aya-current. When there are no other snippets available aya-current will be set to "".

aya-next-in-history & aya-previous-in-history

Set aya-current to the next or previous in aya-history.

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.

aya-persist-snippet-from-history

Functionally equivalent to aya-persist-snippet but using a snippet selected from aya-history

More Repositories

1

swiper

Ivy - a generic completion frontend for Emacs, Swiper - isearch with an overview, and more. Oh, man!
Emacs Lisp
2,284
star
2

hydra

make Emacs bindings that stick around
Emacs Lisp
1,818
star
3

avy

Jump to things in Emacs tree-style
Emacs Lisp
1,704
star
4

lispy

Short and sweet LISP editing
Emacs Lisp
1,195
star
5

org-download

Drag and drop images to Emacs org-mode
Emacs Lisp
1,055
star
6

ace-window

Quickly switch windows in Emacs
Emacs Lisp
968
star
7

oremacs

My Emacs config
Emacs Lisp
296
star
8

pamparam

Simple and fast flashcards for Emacs
Emacs Lisp
228
star
9

ace-link

Quickly follow links in Emacs
Emacs Lisp
210
star
10

lpy

Minimal Python IDE for GNU Emacs
Emacs Lisp
185
star
11

define-word

Display the definition of word at point in Emacs
Emacs Lisp
154
star
12

tiny

Quickly generate linear ranges in Emacs
Emacs Lisp
143
star
13

worf

vi-like bindings for org-mode
Emacs Lisp
132
star
14

function-args

C++ completion for GNU Emacs
Emacs Lisp
120
star
15

orca

ORg CApture
Emacs Lisp
104
star
16

swiper-helm

Helm version of swiper
Emacs Lisp
79
star
17

helm-make

Select a Makefile target with helm.
Emacs Lisp
62
star
18

plain-org-wiki

Simple jump-to-org-files in a directory package
Emacs Lisp
53
star
19

make-it-so

Transform files with Makefile recipes
Emacs Lisp
47
star
20

org-fu

My org-capture and org-protocol setup
Emacs Lisp
37
star
21

cook

Makefile -> Cookbook.py
Python
36
star
22

whicher

Audit and install the programs that your Emacs config depends on
Emacs Lisp
23
star
23

eclipse-theme

Eclipse theme for Emacs
Emacs Lisp
21
star
24

orly

Additional Org-mode link types and completion for them
Emacs Lisp
18
star
25

helm-j-cheatsheet

Quick J reference for Emacs
Emacs Lisp
17
star
26

zoutline

Emacs library for outlines
Emacs Lisp
16
star
27

gtk-pomodoro-indicator

A simple pomodoro indicator for Unity/GTK tray
Python
16
star
28

elf-mode

Show symbol list when opening a binary file in Emacs
Emacs Lisp
13
star
29

matlab-mode

My fork of matlab-mode
Emacs Lisp
13
star
30

short-lambda

Clojure-style anonymous function literal for Elisp
Emacs Lisp
12
star
31

centimacro

Assign multiple macros as global key bindings in Emacs
Emacs Lisp
12
star
32

xlx

XML <-> LISP converter
Common Lisp
11
star
33

eltex

Write LaTeX in Emacs Lisp
Emacs Lisp
10
star
34

headlong

reckless completion for Emacs
Emacs Lisp
10
star
35

semimap

.Xmodmap that uses semicolon as a modifier
8
star
36

dired-guess

Associate file extensions with programs that can open them for dired
Emacs Lisp
8
star
37

cc-chainsaw

A few tricks to make c++-mode go
Emacs Lisp
7
star
38

ukrainian-holidays

Ukrainian holidays for Emacs calendar
Emacs Lisp
7
star
39

org-mode

http://orgmode.org/org-mode.git
Emacs Lisp
6
star
40

profile-dotemacs

My mirror of http://www.randomsample.de/profile-dotemacs.el
Emacs Lisp
6
star
41

abel

abbrevs for Elisp
Emacs Lisp
5
star
42

touchpad

Toggle touchpad on/off
Python
5
star
43

cedet

My mirror of CEDET (http://cedet.sourceforge.net/).
Emacs Lisp
5
star
44

abo-abo.github.io

HTML
4
star
45

ido-occasional

Use ido where you choose
Emacs Lisp
4
star
46

netherlands-holidays

Netherlands holidays for Emacs calendar
Emacs Lisp
4
star
47

latex-wrap

Wrap selection with a LaTeX environment
Emacs Lisp
4
star
48

simple-benchmark

My notes comparing several PL on a simple benchmark
C++
4
star
49

ciao

Emacs Lisp
4
star
50

xkb-indicator

GTK indicator that allows to run setxkbmap via a keybinding
Python
2
star
51

malk

a simple multi-collection completion demo
Emacs Lisp
1
star
52

soap

Smart Operator a posteriori
Emacs Lisp
1
star