1 #+SETUPFILE: org-setup.inc
3 #+TITLE: Writing snippets
7 ** Quickly finding snippets
9 There are some ways you can quickly find a snippet file:
11 - =M-x yas-new-snippet=
13 Prompts you for a snippet name, then tries to guess a suitable
14 directory to store it, prompting you for creation if it does not
15 exist. Finally, places you in a new buffer set to =snippet-mode= so
16 you can write your snippet.
18 - =M-x yas-find-snippets=
20 Lets you find the snippet file in the directory the snippet was
21 loaded from (if it exists) like =find-file-other-window=. The
22 directory searching logic is similar to =M-x yas-new-snippet=.
24 - =M-x yas-visit-snippet-file=
26 Prompts you for possible snippet expansions like
27 [[sym:yas-insert-snippet][=yas-insert-snippet=]], but instead of expanding it, takes you directly
28 to the snippet definition's file, if it exists.
30 Once you find this file it will be set to =snippet-mode= (see ahead) and
31 you can start editing your snippet.
33 ** Using the =snippet-mode= major mode
35 There is a major mode =snippet-mode= to edit snippets. You can set the
36 buffer to this mode with =M-x snippet-mode=. It provides reasonably
37 useful syntax highlighting.
39 Two commands are defined in this mode:
41 - =M-x yas-load-snippet-buffer=
43 When editing a snippet, this loads the snippet into the correct
44 mode and menu. Bound to =C-c C-c= by default while in
47 - =M-x yas-tryout-snippet=
49 When editing a snippet, this opens a new empty buffer, sets it to
50 the appropriate major mode and inserts the snippet there, so you
51 can see what it looks like. This is bound to =C-c C-t= while in
54 There are also /snippets for writing snippets/: =vars=, =$f= and =$m=
59 A file defining a snippet generally contains the template to be
62 Optionally, if the file contains a line of =# --=, the lines above it
63 count as comments, some of which can be /directives/ (or meta data).
64 Snippet directives look like =# property: value= and tweak certain
65 snippets properties described below. If no =# --= is found, the whole
66 file is considered the snippet template.
68 Here's a typical example:
71 # contributor: pluskid <pluskid@gmail.com>
77 Here's a list of currently supported directives:
79 ** =# key:= snippet abbrev
81 This is the probably the most important directive, it's the abbreviation
82 you type to expand a snippet just before hitting [[sym:yas-trigger-key][=yas-trigger-key=]]. If
83 you don't specify this the snippet will not be expandable through the
86 ** =# name:= snippet name
88 This is a one-line description of the snippet. It will be displayed in
89 the menu. It's a good idea to select a descriptive name for a snippet --
90 especially distinguishable among similar snippets.
92 If you omit this name it will default to the file name the snippet was
95 ** =# condition:= snippet condition
97 This is a piece of Emacs-lisp code. If a snippet has a condition, then
98 it will only be expanded when the condition code evaluate to some
101 See also [[sym:yas-buffer-local-condition][=yas-buffer-local-condition=]] in
102 [[./snippet-expansion.org][Expanding snippets]]
104 ** =# group:= snippet menu grouping
106 When expanding/visiting snippets from the menu-bar menu, snippets for a
107 given mode can be grouped into sub-menus . This is useful if one has too
108 many snippets for a mode which will make the menu too long.
110 The =# group:= property only affect menu construction (See
111 [[./snippet-menu.org][the YASnippet menu]]) and the same effect can be
112 achieved by grouping snippets into sub-directories and using the
113 =.yas-make-groups= special file (for this see
114 [[./snippet-organization.org][Organizing Snippets]]
116 Refer to the bundled snippets for =ruby-mode= for examples on the
117 =# group:= directive. Group can also be nested, e.g.
118 =control structure.loops= tells that the snippet is under the =loops=
119 group which is under the =control structure= group.
121 ** =# expand-env:= expand environment
123 This is another piece of Emacs-lisp code in the form of a =let= /varlist
124 form/, i.e. a list of lists assigning values to variables. It can be
125 used to override variable values while the snippet is being expanded.
127 Interesting variables to override are [[sym:yas-wrap-around-region][=yas-wrap-around-region=]] and
128 [[sym:yas-indent-line][=yas-indent-line=]] (see [[./snippet-expansion.org][Expanding Snippets]]).
130 As an example, you might normally have [[sym:yas-indent-line][=yas-indent-line=]] set to '=auto=
131 and [[sym:yas-wrap-around-region][=yas-wrap-around-region=]] set to =t=, but for this particularly
132 brilliant piece of ASCII art these values would mess up your hard work.
137 # expand-env: ((yas-indent-line 'fixed) (yas-wrap-around-region 'nil))
151 ** =# binding:= direct keybinding
153 You can use this directive to expand a snippet directly from a normal
154 Emacs keybinding. The keybinding will be registered in the Emacs keymap
155 named after the major mode the snippet is active for.
157 Additionally a variable [[sym:yas-prefix][=yas-prefix=]] is set to to the prefix argument
158 you normally use for a command. This allows for small variations on the
159 same snippet, for example in this "html-mode" snippet.
163 # binding: C-c C-c C-m
165 <p>`(when yas-prefix "\n")`$0`(when yas-prefix "\n")`</p>
168 This binding will be recorded in the keymap =html-mode-map=. To expand a
169 paragraph tag newlines, just press =C-u C-c C-c C-m=. Omitting the =C-u=
170 will expand the paragraph tag without newlines.
172 ** =# contributor:= snippet author
174 This is optional and has no effect whatsoever on snippet functionality,
179 The syntax of the snippet template is simple but powerful, very similar
184 Arbitrary text can be included as the content of a template. They are
185 usually interpreted as plain text, except =$= and ==. You need to
186 use \` to escape them: =\$= and =\=. The \` itself may also needed to be
187 escaped as =\\= sometimes.
189 ** Embedded Emacs-lisp code
191 Emacs-Lisp code can be embedded inside the template, written inside
192 back-quotes (==). The lisp forms are evaluated when the snippet is
193 being expanded. The evaluation is done in the same buffer as the
194 snippet being expanded.
196 Here's an example for c-mode` to calculate the header file guard
200 #ifndef ${1:_`(upcase (file-name-nondirectory (file-name-sans-extension (buffer-file-name))))`_H_}
208 From version 0.6, snippets expansions are run with some special
209 Emacs-lisp variables bound. One of this is [[sym:yas-selected-text][=yas-selected-text=]]. You can
210 therefore define a snippet like:
214 `yas-selected-text`$0
218 to "wrap" the selected region inside your recently inserted snippet.
219 Alternatively, you can also customize the variable
220 [[sym:yas-wrap-around-region][=yas-wrap-around-region=]] to =t= which will do this automatically.
224 Tab stops are fields that you can navigate back and forth by =TAB= and
225 =S-TAB=. They are written by =$= followed with a number. =$0= has the
226 special meaning of the /exit point/ of a snippet. That is the last place
227 to go when you've traveled all the fields. Here's a typical example:
234 ** Placeholder fields
236 Tab stops can have default values -- a.k.a placeholders. The syntax is
243 They acts as the default value for a tab stop. But when you firstly
244 type at a tab stop, the default value will be replaced by your typing.
245 The number can be omitted if you don't want to create [[mirrors]] or
246 [[transformations]] for this field.
250 We refer the tab stops with placeholders as a /field/. A field can have
251 mirrors. Its mirrors will get updated when you change the text of a
252 field. Here's an example:
255 \begin{${1:enumerate}}
260 When you type "document" at =${1:enumerate}=, the word "document" will
261 also be inserted at =\end{$1}=. The best explanation is to see the
262 screencast([[http://www.youtube.com/watch?v=vOj7btx3ATg][YouTube]] or [[http://yasnippet.googlecode.com/files/yasnippet.avi][avi video]]).
264 The tab stops with the same number to the field act as its mirrors. If
265 none of the tab stops has an initial value, the first one is selected as
266 the field and others mirrors.
268 ** Mirrors with <<transformations>>
270 If the value of an =${n:=-construct starts with and contains =$(=, then
271 it is interpreted as a mirror for field =n= with a transformation. The
272 mirror's text content is calculated according to this transformation,
273 which is Emacs-lisp code that gets evaluated in an environment where the
274 variable =text= (or [[sym:yas-text][=yas-text=]]) is bound to the text content (string)
275 contained in the field =n=.Here's an example for Objective-C:
283 - (void)set${2:$(capitalize text)}:($1)aValue
286 $2 = [aValue retain];
291 Look at =${2:$(capitalize text)}=, it is a mirror with transformation
292 instead of a field. The actual field is at the first line: =${2:foo}=.
293 When you type text in =${2:foo}=, the transformation will be evaluated
294 and the result will be placed there as the transformed text. So in this
295 example, if you type "baz" in the field, the transformed text will be
296 "Baz". This example is also available in the screencast.
298 Another example is for =rst-mode=. In reStructuredText, the document
299 title can be some text surrounded by "===" below and above. The "==="
300 should be at least as long as the text. So
316 is not. Here's an snippet for rst title:
319 ${1:$(make-string (string-width text) ?\=)}
321 ${1:$(make-string (string-width text) ?\=)}
326 ** Fields with transformations
328 From version 0.6 on, you can also have lisp transformation inside
329 fields. These work mostly mirror transformations but are evaluated when
330 you first enter the field, after each change you make to the field and
331 also just before you exit the field.
333 The syntax is also a tiny bit different, so that the parser can
334 distinguish between fields and mirrors. In the following example
336 : #define "${1:mydefine$(upcase yas-text)}"
338 =mydefine= gets automatically upcased to =MYDEFINE= once you enter the
339 field. As you type text, it gets filtered through the transformation
342 Note that to tell this kind of expression from a mirror with a
343 transformation, YASnippet needs extra text between the =:= and the
344 transformation's =$=. If you don't want this extra-text, you can use two
347 : #define "${1:$$(upcase yas-text)}"
349 Please note that as soon as a transformation takes place, it changes the
350 value of the field and sets it its internal modification state to
351 =true=. As a consequence, the auto-deletion behaviour of normal fields
352 does not take place. This is by design.
354 ** Choosing fields value from a list and other tricks
356 As mentioned, the field transformation is invoked just after you enter
357 the field, and with some useful variables bound, notably
358 [[sym:yas-modified-p][=yas-modified-p=]] and [[sym:yas-moving-away-p][=yas-moving-away-p=]]. Because of this feature you
359 can place a transformation in the primary field that lets you select
360 default values for it.
362 The [[sym:yas-choose-value][=yas-choose-value=]] does this work for you. For example:
365 <div align="${2:$$(yas-choose-value '("right" "center" "left"))}">
370 See the definition of [[sym:yas-choose-value][=yas-choose-value=]] to see how it was written using
373 Here's another use, for LaTeX-mode, which calls reftex-label just as you
374 enter snippet field 2. This one makes use of [[sym:yas-modified-p][=yas-modified-p=]] directly.
377 \section{${1:"Titel der Tour"}}%
379 \label{{2:"waiting for reftex-label call..."$(unless yas-modified-p (reftex-label nil 'dont-
383 The function [[sym:yas-verify-value][=yas-verify-value=]] has another neat trick, and makes use
384 of [[sym:yas-moving-away-p][=yas-moving-away-p=]]. Try it and see! Also, check out this [[http://groups.google.com/group/smart-snippet/browse_thread/thread/282a90a118e1b662][thread]]
386 ** Nested placeholder fields
388 From version 0.6 on, you can also have nested placeholders of the type:
391 <div${1: id="${2:some_id}"}>$0</div>
394 This allows you to choose if you want to give this =div= an =id=
395 attribute. If you tab forward after expanding it will let you change
396 "some\_id" to whatever you like. Alternatively, you can just press =C-d=
397 (which executes [[sym:yas-skip-and-clear-or-delete-char][=yas-skip-and-clear-or-delete-char=]]) and go straight to
400 By the way, =C-d= will only clear the field if you cursor is at the
401 beginning of the field /and/ it hasn't been changed yet. Otherwise, it
402 performs the normal Emacs =delete-char= command.