Important: This documentation applies to the SVN trunk of YASnippet, which you get here. Documentation for other versions can be found here.
Contents
Snippet definitions are stored in files in the filesystem. Unless you use the simpler bundle version), these are arranged so that YASnippet can load them into snippet tables. The triggering mechanisms (see Expanding snippets) will look up these snippet tables and (hopefully) expand the snippet you intended.
The non-bundle version of YASnippet, once unpacked, comes with a full directory of snippets, which you can copy somewhere and use. You can also create or download more directories.
Once these directories are in place reference them in the variable yas-snippet-dirs and then load YASnippet as usual:
;; Develop and keep personal snippets under ~/emacs.d/mysnippets
(setq yas-snippet-dirs '("~/emacs.d/mysnippets"))
;; Load yasnippet
(yas-global-mode 1)
The point in using yas-snippet-dirs is considering "~/emacs.d/mysnippets" for snippet development, so you can use commands like yas/new-snippet and others described in section Writing Snippets.
Note: In the past, yas/root-directory is the variable that stores the list of snippet directories. This method is deprecated and only exists for backward-compatibility reason.
Once you've setup yas-snippet-dirs , you can store snippets inside sub-directories of these directories.
Snippet definitions are put in plain text files. They are arranged by sub-directories, and the snippet tables are named after these directories.
The name corresponds to the Emacs mode where you want expansion to take place. For example, snippets for c-mode are put in the c-mode sub-directory.
It's very useful to have certain modes share snippets between themselves. To do this, choose a mode subdirectory and place a .yas-parents containing a whitespace-separated list of other mode names. When you reload those modes become parents of the original mode.
$ tree
.
|-- c-mode
| |-- .yas-parents # contains "cc-mode text-mode"
| `-- printf
|-- cc-mode
| |-- for
| `-- while
|-- java-mode
| |-- .yas-parents # contains "cc-mode text-mode"
| `-- println
`-- text-mode
|-- email
`-- time
If you place an empty plain text file .yas-make-groups inside one of the mode directories, the names of these sub-directories are considered groups of snippets and The YASnippet Menu is organized much more cleanly, as you can see in the image.
Another alternative way to achieve this is to place a # group: directive inside the snippet definition. See Writing Snippets.
$ tree ruby-mode/
ruby-mode/
|-- .yas-make-groups
|-- collections
| |-- each
| `-- ...
|-- control structure
| |-- forin
| `-- ...
|-- definitions
| `-- ...
`-- general
`-- ...
The most convenient way to define snippets for YASnippet is to put them in a directory arranged by the mode and use yas/load-directory to load them.
However, this might slow down the Emacs start-up speed if you have many snippets. You can use yas/define-snippets to define a bunch of snippets for a particular mode in an Emacs-lisp file.
Since this is hard to maintain, there's a better way: define your snippets in directory and then call M-x yas/compile-bundle to compile it into a bundle file when you modified your snippets.
The release bundle of YASnippet is produced by yas/compile-bundle. The bundle uses yas/define-snippets to define snippets. This avoids the IO and parsing overhead when loading snippets.
Further more, the generated bundle is a stand-alone file not depending on yasnippet.el. The released bundles of YASnippet are all generated this way.
See the internal documentation for these functions
List of directories that store the snippets for each major mode.
The first element of the list is always the user-created snippets directory. Other directories are used for bulk reloading of all snippets using yas/reload-all .
If non-nil, don't derive tab triggers from filenames.
This means a snippet without a # key: directive wont have a tab trigger.