Adding Custom Snippets
helm-yas-create-snippet-on-region is an effective way to create snippets in addition to those Spacemacs provides.
Select a code block that you often create and using the yasnippets tools to create your own generic template
vfor visual select and highlight the text for the snippet. Or use
SPC vand grow the selected region
SPC SPC helm-yas-create-snippet-on-regioncreates a new snippet
- Enter the name you wish to refer to this snippet
- Select the major mode you want to add the snippet too (eg. clojure, markdown, html)
- Edit the text and delete any text that is not required
- Add tab points in the snippet if relevant
$1initial cursor position,
$2next cursor position on tab,
$0end cursor position.
yas-tryout-snippetto test the snippet has the right behaviour
yas-load-snippet-buffer-and-closeto load the snippet into the specified major mode and closes the snippet buffer
Edit an existing snippet
SPC SPC yas/visit-snippet-file displays a helm popup listing snippets. Enter a pattern to narrow the list or
C-k to navigate the snippets.
RET to select the snippet.
Edit the snippet and
SPC f s to save file
C-c C-c calls the function
yas-load-snippet-buffer-and-close which loads the snippet into Spacemacs and closes the buffer.
C-c C-t calls
yas-tryout-snippet to test the snippet and check the placement of your tab stops.
How to write a snippet
The basic structure of a snippet template is:
#name : FullNameOfSnippet (not the alias name, the full name shows in autocompletion menu) #contributor: John Stevenson <email@example.com> # -- Add the content you want to replace the snippet name with when it expands
The content can be anything, simple text or more usefully a code strucuture with placeholders
Example: Simple text replacement
As Practicalli develops the book content its useful to highlight sections which are still work in progress. Rather than type the same message over again, a simple snippet called
wip is created.
#name : WorkInProgress #contributor: Practicalli <firstname.lastname@example.org> # -- > **Fixme** work in progress
When you expand this snippet with
M-/ then the snippet name is replaced by the content.
Example: Using tab stops
Yasnippets official snippets repository contains a snippet called
form in the
html-mode. This expands into a html form and jumps from method, id, action and content.
#contributor : Practicalli <email@example.com> #name :<form method="..." id="..." action="..."></form> # -- <form method="$1" id="$2" action="$3"> $0 </form>
When the snippet is expanded, the snippet name is replaced by the content as usual but the cursor is placed at the first tab stop
$1. Each time you press
TAB you move to the next tab stop.
$0 is our exit point from the snippet, so pressing
TAB reverts to the usual behaviour outside of YASnippet.
Testing your snippets
M-x yas-tryout-snippet opens a new empty buffer in the appropriate major mode and inserts the snippet so you can then test it with
M-x yas-load-snippet-buffer to load the new snippet into the correct major mode.
M-x yas-load-snippet-buffer-and-close also loads the new snippet into the correct major mode and kills the snippet buffer (prompting to save first if necessary).
PR for yasnippet key bindings
SPC x s key bindings for yasnipet commands in Spacemacs.
SPC x s e to edit an existing snippet (
SPC x s c to create a snippet.
SPC x s t to try a snippet.
SPC x s l to load a snippet.
SPC x s L to load a snippet and close the snippet buffer
Find out more about YASnippets and autocompletion from the Github repository for Spacemacs autocompletion layer.