5 .. _Organizing Snippets: snippet-organization.html
6 .. _Expanding Snippets: snippet-expansion.html
7 .. _Writing Snippets: snippet-development.html
8 .. _The YASnippet Menu: snippet-menu.html
16 You can use YASnippet to expand snippets in different ways:
18 * By typing an abbrev, the snippet *trigger key*, and then pressing
19 the key defined in ``yas/trigger-key`` (which defaults to
20 "TAB"). This works in buffers where the minor mode
21 ``yas/minor-mode`` is active;
23 * By invoking the command ``yas/insert-snippet`` (either by typing
24 ``M-x yas/insert-snippet`` or its keybinding). This does *not*
25 require ``yas/minor-mode`` to be active.
27 * By using the keybinding associated with an active snippet. This also
28 requires ``yas/minor-mode`` to be active;
30 * By expanding directly from the "YASnippet" menu in the menu-bar
32 * By using hippie-expand
34 * Expanding from emacs-lisp code
39 When ``yas/minor-mode`` is enabled, the keybinding taken from
40 ``yas/trigger-key`` will take effect.
42 ``yas/trigger-key`` invokes ``yas/expand``, which tries to expand a
43 *snippet abbrev* (also known as *snippet key*) before point.
45 The default key is ``"TAB"``, however, you can freely set it to some
48 .. image:: images/minor-mode-indicator.png
51 To enable the YASnippet minor mode in all buffers globally use the
52 command ``yas/global-mode``.
54 When you use ``yas/global-mode`` you can also selectively disable
55 YASnippet in some buffers by setting the buffer-local variable
56 ``yas/dont-active`` in the buffer's mode hook.
58 Trouble when using or understanding the ``yas/trigger-key`` is easily
59 the most controversial issue in YASsnippet. See the `FAQ <faq.html>`_.
64 ``yas/fallback-behaviour`` is a customization variable bound to
65 ``'call-other-command`` by default. If ``yas/expand`` failed to find
66 any suitable snippet to expand, it will disable the minor mode
67 temporarily and find if there's any other command bound the
70 If found, the command will be called. Usually this works very well --
71 when there's a snippet, expand it, otherwise, call whatever command
72 originally bind to the trigger key.
74 However, you can change this behavior by customizing the
75 ``yas/fallback-behavior`` variable. If you set this variable to
76 ``'return-nil``, it will return ``nil`` instead of trying to call the
77 *original* command when no snippet is found.
82 The command ``M-x yas/insert-snippet`` lets you insert snippets at
83 point *for you current major mode*. It prompts you for the snippet
84 key first, and then for a snippet template if more than one template
85 exists for the same key.
87 The list presented contains the snippets that can be inserted at
88 point, according to the condition system. If you want to see all
89 applicable snippets for the major mode, prefix this command with
92 The prompting methods used are again controlled by
93 ``yas/prompt-functions``.
98 See the section of the ``# binding:`` directive in `Writing
102 Expanding from the menu
103 -----------------------
105 See `the YASnippet Menu`_.
107 Expanding with ``hippie-expand``
108 ----------------------------------
110 To integrate with ``hippie-expand``, just put
111 ``yas/hippie-try-expand`` in
112 ``hippie-expand-try-functions-list``. This probably makes more sense
113 when placed at the top of the list, but it can be put anywhere you
116 Expanding from emacs-lisp code
117 ------------------------------
119 Sometimes you might want to expand a snippet directly from you own
120 elisp code. You should call ``yas/expand-snippet`` instead of
121 ``yas/expand`` in this case.
123 As with expanding from the menubar, the condition system and multiple
124 candidates doesn't affect expansion. In fact, expanding from the
125 YASnippet menu has the same effect of evaluating the follow code:
127 .. sourcecode:: common-lisp
129 (yas/expand-snippet template)
131 See the internal documentation on ``yas/expand-snippet`` for more
134 Controlling expansion
135 =====================
140 YASnippet does quite a bit of filtering to find out which snippets are
141 eligible for expanding at the current cursor position.
143 In particular, the following things matter:
145 * Currently loaded snippets tables
147 These are loaded from a directory hierarchy in your file system. See
148 `Organizing Snippets`_. They are named after major modes like
149 ``html-mode``, ``ruby-mode``, etc...
151 * Major mode of the current buffer
153 If the currrent major mode matches one of the loaded snippet tables,
154 then all that table's snippets are considered for expansion. Use
155 ``M-x describe-variable RET major-mode RET`` to find out which major
156 mode you are in currently.
160 Snippet tables defined as the parent of some other eligible table
161 are also considered. This works recursively, i.e. parents of parents
162 of eligible tables are also considered.
164 * Buffer-local ``yas/mode-symbol`` variable
166 This can be used to consider snippet tables whose name does not
167 correspond to a major mode. If you set this variable to a name ,
168 like ``rinari-minor-mode``, you can have some snippets expand only
169 in that minor mode. Naturally, you want to set this conditionally,
170 i.e. only when entering that minor mode, so using a hook is a good
173 .. sourcecode:: common-lisp
175 ;; When entering rinari-minor-mode, consider also the snippets in the
176 ;; snippet table "rails-mode"
177 (add-hook 'rinari-minor-mode-hook
179 (setq yas/mode-symbol 'rails-mode)))
181 * Buffer-local ``yas/buffer-local-condition`` variable
183 This variable provides finer grained control over what snippets can
184 be expanded in the current buffer. The default value won't let you
185 expand snippets inside comments or string literals for example. See
186 `The condition system`_ for more info.
191 Consider this scenario: you are an old Emacs hacker. You like the
192 abbrev-way and set ``yas/trigger-key`` to ``"SPC"``. However,
193 you don't want ``if`` to be expanded as a snippet when you are typing
194 in a comment block or a string (e.g. in ``python-mode``).
196 If you use the ``# condition :`` directive (see `Writing Snippets`_)
197 you could just specify the condition for ``if`` to be ``(not
198 (python-in-string/comment))``. But how about ``while``, ``for``,
199 etc. ? Writing the same condition for all the snippets is just
200 boring. So has a buffer local variable
201 ``yas/buffer-local-condition``. You can set this variable to ``(not
202 (python-in-string/comment))`` in ``python-mode-hook``.
204 Then, what if you really want some particular snippet to expand even
205 inside a comment? This is also possible! But let's stop telling the
206 story and look at the rules:
208 * If ``yas/buffer-local-condition`` evaluate to nil, no snippets will
209 be considered for expansion.
211 * If it evaluates to the a *cons cell* where the ``car`` is the symbol
212 ``require-snippet-condition`` and the ``cdr`` is a symbol (let's
213 call it ``requirement``), then:
215 * Snippets having no ``# condition:`` directive won't be considered;
217 * Snippets with conditions that evaluate to nil (or produce an
218 error) won't be considered;
220 * If the snippet has a condition that evaluates to non-nil (let's
223 * If ``requirement`` is ``t``, the snippet is ready to be
226 * If ``requirement`` is ``eq`` to ``result``, the snippet is ready
229 * Otherwise the snippet won't be considered.
231 * If it evaluates to the symbol ``always``, all snippets are
232 considered for expansion, regardless of any conditions.
234 * If it evaluate to ``t`` or some other non-nil value:
236 * If the snippet has no condition, or has a condition that evaluate
237 to non-nil, it is ready to be expanded.
239 * Otherwise, it won't be considered.
241 In the mentioned scenario, set ``yas/buffer-local-condition`` like
244 .. sourcecode:: common-lisp
246 (add-hook 'python-mode-hook
248 (setq yas/buffer-local-condition
249 '(if (python-in-string/comment)
250 '(require-snippet-condition . force-in-comment)
253 ... and specify the condition for a snippet that you're going to
254 expand in comment to be evaluated to the symbol
255 ``force-in-comment``. Then it can be expanded as you expected, while
256 other snippets like ``if`` still can't expanded in comment.
258 Multiples snippet with the same key
259 -----------------------------------
261 The rules outlined `above <Eligible snippets>`_ can return more than
262 one snippet to be expanded at point.
264 When there are multiple candidates, YASnippet will let you select
265 one. The UI for selecting multiple candidate can be customized through
266 ``yas/prompt-functions`` , which defines your preferred methods of
267 being prompted for snippets.
269 You can customize it with ``M-x customize-variable RET
270 yas/prompt-functions RET``. Alternatively you can put in your
273 .. sourcecode:: common-lisp
275 (setq yas/prompt-functions '(yas/x-prompt yas/dropdown-prompt))
277 Currently there are some alternatives solution with YASnippet.
279 .. image:: images/x-menu.png
282 Use the X window system
283 ~~~~~~~~~~~~~~~~~~~~~~~
285 The function ``yas/x-prompt`` can be used to show a popup menu for you
286 to select. This menu will be part of you native window system widget,
289 * It usually looks beautiful. E.g. when you compile Emacs with gtk
290 support, this menu will be rendered with your gtk theme.
291 * Emacs have little control over it. E.g. you can't use ``C-n``,
293 * This function can't be used when in a terminal.
295 .. image:: images/ido-menu.png
301 You can use functions ``yas/completing-prompt`` for the classic emacs
302 completion method or ``yas/ido-prompt`` for a much nicer looking
303 method. The best way is to try it. This works in a terminal.
305 .. image:: images/dropdown-menu.png
308 Use ``dropdown-menu.el``
309 ~~~~~~~~~~~~~~~~~~~~~~~~
311 The function ``yas/dropdown-prompt`` can also be placed in the
312 ``yas/prompt-functions`` list.
314 This works in both window system and terminal and is customizable, you
315 can use ``C-n``, ``C-p`` to navigate, ``q`` to quit and even press
316 ``6`` as a shortcut to select the 6th candidate.
321 See below for the documentation on variable ``yas/prompt-functions``
323 Customizable Variables
324 ======================
326 ``yas/prompt-functions``
327 ------------------------
329 You can write a function and add it to the ``yas/prompt-functions``
330 list. These functions are called with the following arguments:
332 * PROMPT: A string to prompt the user;
334 * CHOICES: A list of strings or objects;
336 * optional DISPLAY-FN : A function. When applied to each of the
337 objects in CHOICES it will return a string;
339 The return value of any function you put here should be one of
340 the objects in CHOICES, properly formatted with DISPLAY-FN (if
343 * To signal that your particular style of prompting is unavailable at
344 the moment, you can also have the function return nil.
346 * To signal that the user quit the prompting process, you can signal
347 ``quit`` with ``(signal 'quit "user quit!")``
349 ``yas/fallback-behavior``
350 -------------------------
352 How to act when ``yas/expand`` does *not* expand a snippet.
354 ``call-other-command`` means try to temporarily disable YASnippet and
355 call the next command bound to ``yas/trigger-key``.
357 ``return-nil`` means return nil. (i.e. do nothing)
359 An entry (apply COMMAND . ARGS) means interactively call COMMAND, if
360 ARGS is non-nil, call COMMAND non-interactively with ARGS as
363 ``yas/choose-keys-first``
364 -------------------------
366 If non-nil, prompt for snippet key first, then for template.
368 Otherwise prompts for all possible snippet names.
370 This affects ``yas/insert-snippet`` and ``yas/visit-snippet-file``.
372 ``yas/choose-tables-first``
373 ---------------------------
375 If non-nil, and multiple eligible snippet tables, prompts user for
378 Otherwise, user chooses between the merging together of all
381 This affects ``yas/insert-snippet``, ``yas/visit-snippet-file``
386 The default searching strategy is quite powerful. For example, in
387 ``c-mode``, ``bar``, ``foo_bar``, ``"#foo_bar"`` can all be recognized
388 as a snippet key. Furthermore, the searching is in that order. In
389 other words, if ``bar`` is found to be a key to some *valid* snippet,
390 then that snippet is expanded and replaces the ``bar``. Snippets
391 pointed to by ``foo_bar`` and ``"#foobar`` won't be considered.
393 However, this strategy can also be customized easily from the
394 ``yas/key-syntaxes`` variable. It is a list of syntax rules, the
395 default value is ``("w" "w_" "w_." "^ ")``. Which means search the
396 following thing until found one:
399 * a symbol. In lisp, ``-`` and ``?`` can all be part of a symbol.
400 * a sequence of characters of either word, symbol or punctuation.
401 * a sequence of characters of non-whitespace characters.
403 But you'd better keep the default value unless you want to understand
404 how Emacs's syntax rules work...