Contents
A file defining a snippet may just contain the template for the snippet. Optionally it can also contains some meta data for the snippet as well as comments.
Generally speaking, if the file contains a line of # --, then all contents above that line are considered as meta data and comments; below are template. Or else the whole file content is considered as the template.
Here's a typical example:
#contributor : pluskid <pluskid@gmail.com>
#name : __...__
# --
__${init}__
Meta data are specified in the syntax of
#data-name : data value
Any other text above # -- is considered as comment and ignored. Here's a list of currently supported meta data:
When user press the yas/trigger-key, YASnippet try to find a proper snippet to expand. The strategy to find such a snippet is explained here.
YASnippet search from current point backward trying to find the snippet to be expanded. The default searching strategy is quite powerful. For example, in c-mode, "bar", "foo_bar", "#foo_bar" can all be recognized as a template key. Further more, the searching is in that order. In other words, if "bar" is found to be a key to some valid snippet, then "foo_bar" and "#foobar" won't be searched.
However, this strategy can also be customized easily from the yas/key-syntaxes variable. It is a list of syntax rules, the default value is ("w" "w_" "w_." "^ "). Which means search the following thing until found one:
But you'd better keep the default value unless you understand what Emacs's syntax rule mean.
I write forked snippet.el to make the smart-snippet.el. I call it smart-snippet because a condition can be attached to a snippet. This is really a good idea. However, writing condition for a snippet usually needs good elisp and Emacs knowledge, so it is strange to many user.
Later I write YASnippet and persuade people to use it instead of smart-snippet.el. However, some user still love smart-snippet because it is smart. So I make YASnippet smart. Even smarter than smart-snippet.el. :p
Consider this scenario: you are an old Emacs hacker. You like the abbrev-way and set yas/trigger-key to (kbd "SPC"). However, you don't want if to be expanded as a snippet when you are typing in a comment block or a string (e.g. in python-mode).
It's OK, just specify the condition for if to be (not (python-in-string/comment)). But how about while, for, etc. ? Writing the same condition for all the snippets is just boring. So YASnippet introduce a buffer local variable yas/buffer-local-condition. You can set this variable to (not (python-in-string/comment)) in python-mode-hook. There's no way to do this in smart-snippet.el!
Then, what if you really want some snippet even in comment? This is also possible! But let's stop telling the story and look at the rules:
So set yas/buffer-local-condition like this
(add-hook 'python-mode-hook
'(lambda ()
(setq yas/buffer-local-condition
'(if (python-in-string/comment)
'(require-snippet-condition . force-in-comment)
t))))
And specify the condition for a snippet that you're going to expand in comment to be evaluated to the symbol force-in-comment. Then it can be expanded as you expected, while other snippets like if still can't expanded in comment.
There can be multiple snippet bind to the same key. If you define a snippet with a key that is already used, you'll overwrite the original snippet definition. However, you can add a different postfix to the key.
In general, the extension (consider a file name) is ignored when defining a snippet. So def, def.1 and def.mine will all be valid candidates when the key is def.
When there are multiple candidates, YASnippet will let you select one. The UI for selecting multiple candidate can be customized. There're two variable related:
From version 0.6 of YASnippet this has changed significantly. A new customization variable, called yas/prompt-functions defines your preferred method of being prompted for snippets.
You can customize it with M-x customize-variable RET yas/prompt-functions RET. Alternatively you can put in your emacs-file:
(setq yas/prompt-functions '(yas/x-prompt yas/dropdown-prompt))
Currently there are some alternatives solution with YASnippet.
The function yas/x-prompt can be used to show a popup menu for you to select. This menu will be part of you native window system widget, which means:
You can use functions yas/completing-prompt for the classic emacs completion method or yas/ido-prompt for a much nicer looking method. The best way is to try it. This works in a terminal.
YASnippet is implemented as a minor-mode (yas/minor-mode). The trigger key yas/trigger-key is defined in yas/minor-mode-map to call yas/expand to try to expand a snippet.
When yas/minor-mode is enabled, the trigger key will take effect. The default key is (kbd "TAB"), however, you can freely set it to some other key.
In version 0.5, YASnippet add a hook to after-change-major-mode-hook to enable yas/minor-mode in every buffer. This works fine for most modes, however, some mode doesn't follow the Emacs convention and doens't call this hook. You can either explicitly hook for those mode or just add it to yas/extra-mode-hooks to let YASnippet do it for you:
(require 'yasnippet)
(add-to-list 'yas/extra-mode-hooks
'ruby-mode-hook)
(yas/initialize)
Note that should be put after (require 'yasnippet) and before (yas/initialize). Further more, you may report it to me, I'll add that to the default value.
In version 0.6, just use yas/global-mode to enable YASnippet in all major modes. Or put yas/minor-mode-on in that modes hook. See the FAQ.
If yas/expand failed to find any suitable snippet to expand, it will disable the minor mode temporarily and find if there's any other command bind the yas/trigger-key. If found, the command will be called. Usually this works very well -- when there's a snippet, expand it, otherwise, call whatever command originally bind to the trigger key.
However, you can change this behavior by customizing the yas/fallback-behavior variable. If you set this variable to 'return-nil, it will return nil instead of trying to call the original command when no snippet is found. This is useful when you would like YASnippet to work with other extensions, e.g. hippie-expand. I'm also glad to tell you that integration with hippie-expand is already included in YASnippet.
To integrate with hippie-expand, just put yas/hippie-try-expand in hippie-expand-try-functions-list. Personally I would like to put in front of the list, but it can be put anywhere you prefer.
When you use the trigger key (so yas/expand) to expand a snippet, the key for the snippet is deleted before the template for the snippet is inserted.
However, there're other ways to insert a snippet.
The command M-x yas/insert-snippet lets you insert snippets at point for you current major mode. It prompts you for the snippet key first, and then for a snippet template if more than one template exists for the same key.
The list presented contains the snippets that can be inserted at point, according to the condition system. If you want to see all applicable snippets for the major mode, prefix this command with C-u.
The prompting methods used are again controlled by yas/prompt-functions.
Sometimes you might want to expand a snippet directly by calling a functin from elisp code. You should call yas/expand-snippet instead of yas/expand in this case.
As with expanding from the menubar, condition system and multiple candidates won't exists here. In fact, expanding from menubar has the same effect of evaluating the follow code:
(yas/expand-snippet (point) (point) template)
Where template is the template of a snippet. It is never required to belong to any snippet -- you can even make up it on the fly. The 1st and 2nd parameter defines the region to be deleted after YASnippet inserted the template. It is used by yas/expand to indicate the region of the key. There's usually no need to delete any region when we are expanding a snippet from elisp code, so passing two (point) is fine. Note only (point) will be fine because the 1st parameter also indicate where to insert and expand the template.
Many people miss the indenting feature of smart-snippet: when you place a $> in your snippet, an (indent-according-to-mode) will be executed there to indent the line. So you'll not need to hard-code the indenting in the snippet template, and it will be very convenient when you need to work with several different project where coding styles are different.
The reason why this feature wasn't added to YASnippet until after 0.5.6 is that it doesn't work well for all modes. In some cases (e.g. python-mode), calling indent-according-to-mode will break the overlays created by YASnippet.
However, since many people asked for this feature, I finally added this to YASnippet. Here's an example of the usage:
for (${int i = 0}; ${i < 10}; ${++i})
{$>
$0$>
}$>
In 0.6.0 You should not need to use this feature although it's supported for backward compatibility. Just set yas/indent-line to 'auto.