# Creating a custom Yasnippet with conditions

I have a yasnippet that is conflicting with multiple-cursors because it is not exiting immediately. When multiple-cursors is not active, I would like the snippet to pause and permit me to enter the data. Can I have an example, please, of a yasnippet that uses some elisp conditions:

For example,

(if mc-mode
\hspace*{$0} \hspace*{$1})


Yasnippet will accept elisp (including, but limited to conditions) provided that it is surrounded by backticks. In the following example, $0 will cause the snippet to automatically exit when mc-mode is t; whereas, $1 will cause a pause at that location for user input when mc-mode is nil.

(if mc-mode
"\\\\hspace*{$0}" "\\\\hspace*{$1}")


You can use the #condition directive to provide an elisp predicate. The snippet will only be expanded when the condition code you provide evaluates to non-nil. So, you could simply add this to your snippet:

# condition: (mc-mode)

• I interpreted, perhaps incorrectly (?), the term expand to mean that yasnippet does what it is supposed to do. And, that the #condition is designed to prevent yasnippet from functioning if the condition is satisfied. Does the #condition affect the whole kitten-kaboodle (i.e., nothing at all should happen), or just whether to pause at position \$1 to wait for user input? Dec 18, 2014 at 5:55
• If the condition returns nil, the snippet will not be eligible for expansion. So, when #condition is nil, yas-expand behaves as if the snippet didn't exist. What it does instead of snippet expansion(assuming no other snippet is a match either) depends on the fallback behaviour. Dec 18, 2014 at 6:02

Another condition example that is simple enough but might be pretty handy in many projects with clear and stable structure.

# -*- mode: snippet -*-
# name: controller snippet foo
# condition: (cl-search "app/controllers/" buffer-file-name)
A snippet that activates only when:
(1) you're in corresponding major-mode
(2) your buffer file name is within "app/controllers/" path.


This should be pretty handy in case one has lots of snippets and want to exclude some of the based on the file directories they're called from.