1 #+SETUPFILE: org-setup.inc
6 This section describes how YASnippet chooses snippets for expansion at point.
8 Maybe, you'll want some snippets to be expanded in a particular
9 mode, or only under certain conditions, or be prompted using
11 ** Triggering expansion
13 You can use YASnippet to expand snippets in different ways:
15 - When [[sym:yas-minor-mode][=yas-minor-mode=]] is active:
16 - Type the snippet's *trigger key* then calling [[sym:yas-expand][=yas-expand=]]
17 (bound to =TAB= by default).
19 - Use the snippet's *keybinding*.
21 - By expanding directly from the "YASnippet" menu in the menu-bar
25 - Call [[sym:yas-insert-snippet][=yas-insert-snippet=]] (use =M-x yas-insert-snippet== or its
26 keybinding =C-c & C-s=).
28 - Use m2m's excellent auto-complete
29 TODO: example for this
31 - Expanding from emacs-lisp code
35 [[sym:yas-expand][=yas-expand=]] tries to expand a /snippet abbrev/ (also known as
36 /snippet key/) before point.
38 When [[sym:yas-minor-mode][=yas-minor-mode=]] is enabled, it binds [[sym:yas-expand][=yas-expand=]] to =TAB= and
39 =<tab>= by default, however, you can freely set it to some other key:
41 #+begin_src emacs-lisp :exports code
42 (define-key yas-minor-mode-map (kbd "<tab>") nil)
43 (define-key yas-minor-mode-map (kbd "TAB") nil)
44 (define-key yas-minor-mode-map (kbd "<the new key>") 'yas-expand)
47 To enable the YASnippet minor mode in all buffers globally use the
48 command [[sym:yas-global-mode][=yas-global-mode=]]. This will enable a modeline indicator,
51 [[./images/minor-mode-indicator.png]]
53 When you use [[sym:yas-global-mode][=yas-global-mode=]] you can also selectively disable
54 YASnippet in some buffers by setting the buffer-local variable
55 [[sym:yas-dont-active][=yas-dont-active=]] in the buffer's mode hook.
57 **** Fallback bahaviour
59 [[sym:yas-fallback-behaviour][=yas-fallback-behaviour=]] is a customization variable bound to
60 '=call-other-command= by default. If [[sym:yas-expand][=yas-expand=]] failed to find any
61 suitable snippet to expand, it will disable the minor mode temporarily
62 and find if there's any other command bound the [[sym:yas-trigger-key][=yas-trigger-key=]].
64 If found, the command will be called. Usually this works very well
65 --when there's a snippet, expand it, otherwise, call whatever command
66 originally bind to the trigger key.
68 However, you can change this behavior by customizing the
69 [[sym:yas-fallback-behavior][=yas-fallback-behavior=]] variable. If you set this variable to
70 '=return-nil=, it will return =nil= instead of trying to call the
71 /original/ command when no snippet is found.
75 The command =M-x yas-insert-snippet= lets you insert snippets at point
76 /for you current major mode/. It prompts you for the snippet key first,
77 and then for a snippet template if more than one template exists for the
80 The list presented contains the snippets that can be inserted at point,
81 according to the condition system. If you want to see all applicable
82 snippets for the major mode, prefix this command with =C-u=.
84 The prompting methods used are again controlled by
85 [[sym:yas-prompt-functions][=yas-prompt-functions=]].
87 *** Snippet keybinding
89 See the section of the =# binding:= directive in
90 [[./snippet-development.org][Writing Snippets]].
92 *** Expanding from the menu
94 See [[./snippet-menu.org][the YASnippet Menu]].
96 *** Expanding with =hippie-expand=
98 To integrate with =hippie-expand=, just put [[sym:yas-hippie-try-expand][=yas-hippie-try-expand=]] in
99 =hippie-expand-try-functions-list=. This probably makes more sense when
100 placed at the top of the list, but it can be put anywhere you prefer.
102 *** Expanding from emacs-lisp code
104 Sometimes you might want to expand a snippet directly from you own elisp
105 code. You should call [[sym:yas-expand-snippet][=yas-expand-snippet=]] instead of [[sym:yas-expand][=yas-expand=]] in
108 As with expanding from the menubar, the condition system and multiple
109 candidates doesn't affect expansion. In fact, expanding from the
110 YASnippet menu has the same effect of evaluating the follow code:
112 See the internal documentation on [[sym:yas-expand-snippet][=yas-expand-snippet=]] for more
115 ** Controlling expansion
117 *** Eligible snippets
119 YASnippet does quite a bit of filtering to find out which snippets are
120 eligible for expanding at the current cursor position.
122 In particular, the following things matter:
124 - Currently loaded snippets tables
126 These are loaded from a directory hierarchy in your file system. See
127 [[./snippet-organization.org][Organizing Snippets]]. They are named
128 after major modes like =html-mode=, =ruby-mode=, etc...
130 - Major mode of the current buffer
132 If the currrent major mode matches one of the loaded snippet tables,
133 then all that table's snippets are considered for expansion. Use
134 =M-x describe-variable RET major-mode RET= to find out which major
135 mode you are in currently.
139 Snippet tables defined as the parent of some other eligible table are
140 also considered. This works recursively, i.e. parents of parents of
141 eligible tables are also considered.
143 - Buffer-local [[sym:yas-mode-symbol][=yas-mode-symbol=]] variable
145 This can be used to consider snippet tables whose name does not
146 correspond to a major mode. If you set this variable to a name , like
147 =rinari-minor-mode=, you can have some snippets expand only in that
148 minor mode. Naturally, you want to set this conditionally, i.e. only
149 when entering that minor mode, so using a hook is a good idea.
151 - Buffer-local [[sym:yas-buffer-local-condition][=yas-buffer-local-condition=]] variable
153 This variable provides finer grained control over what snippets can
154 be expanded in the current buffer. The default value won't let you
155 expand snippets inside comments or string literals for example. See
156 The condition system\_ for more info.
158 *** The condition system
160 Consider this scenario: you are an old Emacs hacker. You like the
161 abbrev-way and set [[sym:yas-trigger-key][=yas-trigger-key=]] to =SPC=. However, you don't want
162 =if= to be expanded as a snippet when you are typing in a comment block
163 or a string (e.g. in =python-mode=).
165 If you use the =# condition := directive (see
166 [[./snippet-development.org][Writing Snippets]]) you could just specify
167 the condition for =if= to be =(not (python-in-string/comment))=. But how
168 about =while=, =for=, etc. ? Writing the same condition for all the
169 snippets is just boring. So has a buffer local variable
170 [[sym:yas-buffer-local-condition][=yas-buffer-local-condition=]]. You can set this variable to
171 =(not (python-in-string/comment))= in =python-mode-hook=.
173 Then, what if you really want some particular snippet to expand even
174 inside a comment? This is also possible! But let's stop telling the
175 story and look at the rules:
177 - If [[sym:yas-buffer-local-condition][=yas-buffer-local-condition=]] evaluate to nil, no snippets will be
178 considered for expansion.
180 - If it evaluates to the a /cons cell/ where the =car= is the symbol
181 =require-snippet-condition= and the =cdr= is a symbol (let's call it
182 =requirement=), then:
184 - Snippets having no =# condition:= directive won't be considered;
186 - Snippets with conditions that evaluate to nil (or produce an
187 error) won't be considered;
189 - If the snippet has a condition that evaluates to non-nil (let's
192 - If =requirement= is =t=, the snippet is ready to be expanded;
194 - If =requirement= is =eq= to =result=, the snippet is ready to
197 - Otherwise the snippet won't be considered.
199 - If it evaluates to the symbol =always=, all snippets are considered
200 for expansion, regardless of any conditions.
202 - If it evaluate to =t= or some other non-nil value:
204 - If the snippet has no condition, or has a condition that evaluate
205 to non-nil, it is ready to be expanded.
207 - Otherwise, it won't be considered.
209 In the mentioned scenario, set [[sym:yas-buffer-local-condition][=yas-buffer-local-condition=]] like this
211 ... and specify the condition for a snippet that you're going to expand
212 in comment to be evaluated to the symbol =force-in-comment=. Then it can
213 be expanded as you expected, while other snippets like =if= still can't
216 *** Multiples snippet with the same key
218 The rules outlined [[Eligible%20snippets][above]] can return more than
219 one snippet to be expanded at point.
221 When there are multiple candidates, YASnippet will let you select one.
222 The UI for selecting multiple candidate can be customized through
223 [[sym:yas-prompt-functions][=yas-prompt-functions=]] , which defines your preferred methods of being
224 prompted for snippets.
226 You can customize it with
227 =M-x customize-variable RET yas-prompt-functions RET=. Alternatively you
228 can put in your emacs-file:
230 Currently there are some alternatives solution with YASnippet.
232 **** Use the X window system
234 [[./images/x-menu.png]]
236 The function [[sym:yas-x-prompt][=yas-x-prompt=]] can be used to show a popup menu for you to
237 select. This menu will be part of you native window system widget, which
240 - It usually looks beautiful. E.g. when you compile Emacs with gtk
241 support, this menu will be rendered with your gtk theme.
242 - Your window system may or may not allow to you use =C-n=, =C-p= to
244 - This function can't be used when in a terminal.
246 **** Minibuffer prompting
248 [[./images/ido-menu.png]]
250 You can use functions [[sym:yas-completing-prompt][=yas-completing-prompt=]] for the classic emacs
251 completion method or [[sym:yas-ido-prompt][=yas-ido-prompt=]] for a much nicer looking method.
252 The best way is to try it. This works in a terminal.
254 **** Use =dropdown-menu.el=
256 [[./images/dropdown-menu.png]]
258 The function [[sym:yas-dropdown-prompt][=yas-dropdown-prompt=]] can also be placed in the
259 [[sym:yas-prompt-functions][=yas-prompt-functions=]] list.
261 This works in both window system and terminal and is customizable, you
262 can use =C-n=, =C-p= to navigate, =q= to quit and even press =6= as a
263 shortcut to select the 6th candidate.
267 See the documentation on variable [[sym:yas-prompt-functions][=yas-prompt-functions=]]