1 #+TITLE: Yet another snippet extension
5 #+STYLE: <link rel="stylesheet" type="text/css" href="stylesheets/styles.css" />
9 #+LINK: smart-snippet http://code.google.com/p/smart-snippet
10 #+LINK: pluskid http://pluskid.lifegoo.org
12 #+LINK: screencast http://www.youtube.com/watch?v=ZCGmZK4V7Sg
13 #+LINK: docs http://capitaomorte.github.com/yasnippet
14 #+LINK: issues https://github.com/capitaomorte/yasnippet/issues
15 #+LINK: googlecode-tracker http://code.google.com/p/yasnippet/issues/list
16 #+LINK: forum http://groups.google.com/group/smart-snippet
21 *YASnippet* is a template system for Emacs. It allows you to type an
22 abbreviation and automatically expand it into function templates. Bundled
23 language templates includes: C, C++, C#, Perl, Python, Ruby, SQL, LaTeX, HTML,
24 CSS and more. The snippet syntax is inspired from TextMate's syntax, you can
25 even [[#import-textmate][import most TextMate snippets]]
27 YASnippet is an original creation of [[pluskid]] who also wrote its predecessor
36 Clone this repository somewhere
39 $ cd ~/.emacs.d/plugins
40 $ git clone https://github.com/capitaomorte/yasnippet
43 Add the following in your =.emacs= file:
45 #+begin_src emacs-lisp :exports code
46 (add-to-list 'load-path
47 "~/.emacs.d/plugins/yasnippet")
52 Add your own snippets to =~/.emacs.d/snippets= by placing files there or
53 invoking [[#yas-new-snippet][=yas-new-snippet=]].
55 ** Import textmate snippets (rails example)
57 :CUSTOM_ID: import-textmate
60 YASnippet lets you use TextMate bundles directly:
63 $ cd ~/.emacs.d/plugins
64 $ git clone https://github.com/capitaomorte/yasnippet
67 $ git submodule update
68 $ gem install plist trollop
69 $ rake convert_bundles # will convert ruby, rails and html bundles from drnic
72 Then, in your =.emacs= file
75 (add-to-list 'load-path
76 "~/.emacs.d/plugins/yasnippet")
78 (setq yas/snippet-dirs '("~/.emacs.d/snippets" "~/.emacs.d/extras/imported"))
82 Open some rails file (model, app, etc) and start using the textmate
83 snippets. Consider that this is a work-in-progress and many snippets/commands
84 might not work. Patches welcome!
86 ** Contributing snippets
88 Please *do not ask me* to add snippets to the default collection under
89 =/snippets=. This collection is considered frozen. By customizing
90 [[#yas-snippet-dirs][=yas-snippet-dirs=]] you can point yasnippet to good
91 snippet collections out there.
93 The =extras/textmate-import.rb= tool can import many actual Textmate
94 snippets. I'm focusing on developing it and the accompanying =yas-setup.el=
95 files that guide it with more difficult importations. The idea is to deprecate
96 =/snippets= and replace it with =extras/imported=.
98 ** Documentation, issues, etc
100 Please refer to the comprehensive [[docs][documentation]] for full
101 customization and support. If you think you've found a bug, please report it
102 on [[issues][the GitHub issue tracker]]. (please **do not** submit new
103 issues to the old [[googlecode-tracker][googlecode tracker]])
105 If you run into problems using YASnippet, or have snippets to contribute,
106 post to the [[forum][yasnippet forum]]. Thank you very much for using
109 * Organizing snippets
113 Snippet collections can be stored in plain text files. They are arranged by
114 sub-directories naming *snippet tables*. These mostly name Emacs major names.
127 The collections are loaded into *snippet tables* which the triggering
128 mechanism (see [[#expand-snippets][Expanding snippets]]) looks up and
129 (hopefully) cause the right snippet to be expanded for you.
131 ** Setting up =yas-snippet-dirs=
133 The emacs variable [[#yas-snippet-dirs][=yas-snippet-dirs=]] tells YASnippet
134 which collections to consider. It's used when you activate
135 [[#yas-global-mode][=yas-global-mode=]] or call
136 [[#yas-reload-all][=yas-reload-all=]] interactively.
138 The default considers:
140 - a personal collection that lives in =~/.emacs.d/snippets=
141 - the bundled collection, taken as a relative path to =yasnippet.el= localtion
143 When you come across other snippet collections, do the following to try them
146 #+begin_src emacs-lisp :exports code
147 ;; Develop in ~/emacs.d/mysnippets, but also
148 ;; try out snippets in ~/Downloads/interesting-snippets
149 (setq yas/snippet-dirs '("~/emacs.d/mysnippets"
150 "~/Downloads/interesting-snippets"))
152 ;; OR, keeping yasnippet's defaults try out ~/Downloads/interesting-snippets
153 (setq yas/snippet-dirs (append yas/snippet-dirs
154 '("~/Downloads/interesting-snippets")))
157 Collections appearing earlier in the list shadow snippets with same names
158 appearing in collections later in the list. [[#yas-new-snippet][=yas-new-snippet=]] always stores
159 snippets in the first collection.
161 ** The =.yas-parents= file
163 It's very useful to have certain modes share snippets between themselves. To do
164 this, choose a mode subdirectory and place a =.yas-parents= containing a
165 whitespace-separated list of other mode names. When you reload those modes
166 become parents of the original mode.
171 | |-- .yas-parents # contains "cc-mode text-mode"
177 | |-- .yas-parents # contains "cc-mode text-mode"
184 ** TODO The =.yas-make-groups= file
186 If you place an empty plain text file =.yas-make-groups= inside one of the
187 mode directories, the names of these sub-directories are considered groups of
188 snippets and [[snippet-menu][the menu]] is organized much more cleanly:
192 Another alternative way to achieve this is to place a =# group:= directive
193 inside the snippet definition. See [[#writing-snippets][Writing Snippets]]
202 |-- control structure
211 Yet another way to create a nice snippet menu is to write into
212 =.yas-make-groups= a menu definition. TODO
214 ** TODO The =.yas-setup.el= file
218 ** TODO The =.yas-compiled-snippet.el= file
222 ** The =.yas-skip= file
227 :CUSTOM_ID: expand-snippets
230 This section describes how YASnippet chooses snippets for expansion at point.
232 Maybe, you'll want some snippets to be expanded in a particular
233 mode, or only under certain conditions, or be prompted using
235 ** Triggering expansion
237 To make a snippet expand after the cursor:
239 * Type an the snippet's *trigger key* then press the key defined in
240 [[#yas-trigger-key][=yas-trigger-key=]] (usually to "TAB").
242 * Use the snippet's *keybinding*.
244 * Call [[#yas-insert-snippet][=yas-insert-snippet=]] (use =M-x
245 yas-insert-snippet== or its keybinding =C-c & C-s=).
247 * By expanding directly from the "YASnippet" menu in the menu-bar
249 * Using hippie-expand
251 * Use m2m's excellent auto-complete
254 #+BEGIN_SRC emacs-lisp :exports results :results value raw
255 (yas--document-symbols 2 `("Interactive functions" . ,#'interactive-form)
256 `("Customization variables" . ,#'(lambda (sym)
258 (get sym 'standard-value))))
259 `("Useful functions" . ,#'fboundp)
260 `("Useful variables" . ,#'boundp))