Skip to content

Create snippets from existing textλ︎

helm-yas-create-snippet-on-region is an effective way to create snippets from existing text.

Select a code block that you often create and using the yasnippets tools to create your own generic template

  1. v for visual select and highlight the text for the snippet. Or use Space v and grow the selected region with v
  2. Space Space helm-yas-create-snippet-on-region creates a new snippet
  3. Enter the name you wish to refer to this snippet
  4. Select the major mode you want to add the snippet too (eg. clojure, markdown, html)
  5. Edit the text and delete any text that is not required
  6. Add tab points in the snippet if relevant
    • $1 initial cursor position, $2 next cursor position on tab, $0 end cursor position.
  7. Ctrl+c Ctrl+t calls yas-tryout-snippet to test the snippet has the right behaviour
  8. Ctrl+c Ctrl+c calls yas-load-snippet-buffer-and-close to load the snippet into the specified major mode and closes the snippet buffer

Edit an existing snippetλ︎

Space Space yas/visit-snippet-file displays a helm popup listing snippets.

Enter a pattern to narrow the list or Ctrl j and Ctrl k to navigate the snippets. Enter to select the snippet.

Edit the snippet and Space 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.