July 23, 2016

YASnippets for faster clojure coding

spacemacs logo - post topic

Yasnippets minimise typing for commonly used Clojure forms and documentation structures, or any other text regularly created. Yasnippets have placeholders that allow jumping (tabbing) to points in the snippet, so specific text can be added to the more generic text of the snippet.

Emacs YASnippet package uses mode-specific snippets that expand to anything from a simple text replacement to an intricate code block structure using placeholders to add specific values to the general form.

See YASnippet in action in this Emacs Yasnippet video.

Add custom snippets for Clojure or any other language you use with Spacemacs / Emacs

Additional ways to speed up typing Clojure code include creating projects from templates or general abbreviations with Emacs Abbrev modeSnippets are also part of Clojure LSP and can be used with any supporting editor.

Using snippets

Start typing the snippet name and press M-/ to yas-expand which replaces the name with the full text of the snippet.

Type defn and press M-/ to expand to the code of a function definition form. Use TAB to move through the placeholders to complete the name, doc-string, arguments and body of the function.

Smartparens is disabled when tabbing through a snippet

List snippets for current major mode

SPC i s (helm-yas) displays a completion buffer that lists the matching snippets available for the current major mode.

C-j and C-k to navigate the list and RET to insert and expand the snippet.

yasnippets yas-helm for clojure mode

Add snippets to completion menu

The auto-completion layer shows a completion popup menu, with matching functions and other names from the REPL or Clojure LSP (clj-kondo). Configure the auto-completion layer to show YASnippets when typing.

Add the auto-completion-enable-snippets-in-popup variable to show snippets in the auto-completion popup.

Add auto-completion-enable-sort-by-usage to show the most commonly used snippets first in the list.

(auto-completion :variables
                 auto-completion-enable-snippets-in-popup t
                 auto-completion-enable-sort-by-usage t)

Matching snippets will now appear in the completion menu whilst typing

Spacemacs helm snippet gitbook example

Default snippets for Clojure

The snippets available in Spacemacs can be found in the yasnippet github repository.

defn, require, let, ifl and for are often used by Practicalli.

NOTE: The test snippet generates a deftest form which by convention uses a name post-fixed with -test. Pressing TAB after test will expand that word into another deftest form within the current deftest form.

Press space after the -test name to prevent test being expanded to a deftest form.

The current snippets for Clojure mode are in the following table, indicating which tab stops they have. mpenet/clojure-snippets includes several additional Clojure snippets (although are not part of yasnippets by default).

SnippetDescriptionTab Stops
benchbenchmark an expression, using the time functionbody
defdef expressionN/A
defmdefmacro expression, with name, doc-string, arguments & body tabstopsname, docstring, args, body
defndefn expression, with name, doc-string, arguments & body tabstopsname, docstring, args, body
fnfn - anonymous functionname, body
forforcondition, body
ififcondition, body
iflif-let - if true, bind a local namebinding, body
importimport java librarylibrary name
isis - clojure test assertionvalue, expected
letlet - bind a local name to a valuename, value, body
mapmapfn, col, col
map.lambdamap with anonymous function #()fn, body
mdocmetadata docstringdocstring
nsns - expression with the current namespace inserted automaticallyN/A
optsdestructure map with :keys, :or for default values, :as for arg vector:key binding, or defaults, :as binding
prprn - print functionstring/value
printprintln - print functionstring/value
reducereduce - reduce expression with an anonymous functionargs, body
require:require expression with library and aliaslibrary, alias
testdeftest expression (not including testing part of a deftest expression)test description, value/expected
trytry & catch expressiontry expression, exception name, body
usedepreciated: use require instead
whenwhenwhen expression, body
whenlwhen-let - local binding on when conditionbinding, body

Summary

Have fun speeding up the writing of your Clojure code (and any other common text structures) in Spacemacs / Emacs.

Consider writing custom snippets to extend those available via Yasnippets.

Article originally published on jr0cket.co.uk and has been migrated to practical.li/blog with some minor updates.

Thank you

practicalli GitHub profile I @practical_li

Tags: emacs snippets spacemacs