]> code.delx.au - gnu-emacs-elpa/blob - doc/index.org
e06f715d52dbe4df6c76f83cfcfcc9a05e7fab02
[gnu-emacs-elpa] / doc / index.org
1 #+TITLE: Yet another snippet extension
2 #+OPTIONS: toc:1
3 #+STARTUP: showall
4
5 #+SETUPFILE: org-setup.inc
6
7 * Quick start
8
9 *YASnippet* is a template system for Emacs. It allows you to type an
10 abbreviation and automatically expand it into function templates. Bundled
11 language templates includes: C, C++, C#, Perl, Python, Ruby, SQL, LaTeX, HTML,
12 CSS and more. The snippet syntax is inspired from TextMate's syntax, you can
13 even [[#import-textmate][import most TextMate snippets]]
14
15 YASnippet is an original creation of [[http://pluskid.lifegoo.org][pluskid]] who also wrote its predecessor
16 [[http://code.google.com/p/smart-snippet][smart-snippet]].
17
18 ** Watch a demo
19
20 On [[http://www.youtube.com/watch?v=ZCGmZK4V7Sg][youtube]].
21
22 ** Installation
23
24 Clone this repository somewhere
25
26 #+begin_example
27 $ cd ~/.emacs.d/plugins
28 $ git clone https://github.com/capitaomorte/yasnippet
29 #+end_example
30
31 Add the following in your =.emacs= file:
32
33 #+begin_src emacs-lisp :exports code
34 (add-to-list 'load-path
35 "~/.emacs.d/plugins/yasnippet")
36 (require 'yasnippet)
37 (yas-global-mode 1)
38 #+end_src
39
40 Add your own snippets to =~/.emacs.d/snippets= by placing files there or
41 invoking [[sym:yas-new-snippet][=yas-new-snippet=]].
42
43 ** Import textmate snippets (rails example)
44 :PROPERTIES:
45 :CUSTOM_ID: import-textmate
46 :END:
47
48 YASnippet lets you use TextMate bundles directly:
49
50 #+begin_example
51 $ cd ~/.emacs.d/plugins
52 $ git clone https://github.com/capitaomorte/yasnippet
53 $ cd yasnippet
54 $ git submodule init
55 $ git submodule update
56 $ gem install plist trollop
57 $ rake convert_bundles # will convert ruby, rails and html bundles from drnic
58 #+end_example
59
60 Then, in your =.emacs= file
61
62 #+begin_example
63 (add-to-list 'load-path
64 "~/.emacs.d/plugins/yasnippet")
65 (require 'yasnippet)
66 (setq yas-snippet-dirs '("~/.emacs.d/snippets" "~/.emacs.d/extras/imported"))
67 (yas-global-mode 1)
68 #+end_example
69
70 Open some rails file (model, app, etc) and start using the textmate
71 snippets. Consider that this is a work-in-progress and many snippets/commands
72 might not work. Patches welcome!
73
74 ** Contributing snippets
75
76 Please *do not ask me* to add snippets to the default collection under
77 =/snippets=. This collection is considered frozen. By customizing
78 [[sym:yas-snippet-dirs][=yas-snippet-dirs=]] you can point yasnippet to good
79 snippet collections out there.
80
81 The =extras/textmate-import.rb= tool can import many actual Textmate
82 snippets. I'm focusing on developing it and the accompanying =yas-setup.el=
83 files that guide it with more difficult importations. The idea is to deprecate
84 =/snippets= and replace it with =extras/imported=.
85
86 ** Documentation
87
88 The documentation has been split into separate parts:
89
90 1. [[file:snippet-organization.org][Organizing Snippets]]
91 #+BEGIN_QUOTE
92 Describes ways to organize your snippets in the hard disk.
93 #+END_QUOTE
94
95 2. [[file:snippet-expansion.org][Expanding Snippets]]
96 #+BEGIN_QUOTE
97 Describes how YASnippet chooses snippets for expansion at point.
98
99 Maybe, you'll want some snippets to be expanded in a particular mode,
100 or only under certain conditions, or be prompted using =ido=, etc...
101 #+END_QUOTE
102
103 3. [[file:snippet-development.org][Writing Snippets]]
104 #+BEGIN_QUOTE
105 Describes the YASnippet definition syntax, which is very close (but
106 not equivalent) to Textmate's. Includes a section about converting
107 TextMate snippets.
108 #+END_QUOTE
109
110 4. [[file:snippet-menu.org][The YASnippet menu]]
111 #+BEGIN_QUOTE
112 Explains how to use the YASnippet menu to explore, learn and modify
113 snippets.
114 #+END_QUOTE
115
116 5. [[file:snippet-reference.org][YASnippet Symbol Reference]]
117 #+BEGIN_QUOTE
118 An automatically generated listing of all YASnippet commands,
119 (customization) variables, and functions.
120 #+END_QUOTE
121
122 ** Bugs, discussion, contributions, etc
123
124 If you think you've found a bug, please report it on [[https://github.com/capitaomorte/yasnippet/issues][the GitHub issue tracker]]
125 (please **do not** submit new issues to the old [[http://code.google.com/p/yasnippet/issues/list][googlecode tracker]]).
126
127 If you run into problems using YASnippet, or have snippets to contribute,
128 post to the [[http://groups.google.com/group/smart-snippet][yasnippet forum]]. Thank you very much for using YASnippet!
129
130 * Organizing snippets
131
132 ** Basic structure
133
134 Snippet collections can be stored in plain text files. They are arranged by
135 sub-directories naming *snippet tables*. These mostly name Emacs major names.
136
137 #+begin_example
138 .
139 |-- c-mode
140 | `-- printf
141 |-- java-mode
142 | `-- println
143 `-- text-mode
144 |-- email
145 `-- time
146 #+end_example
147
148 The collections are loaded into *snippet tables* which the triggering
149 mechanism (see [[#expand-snippets][Expanding snippets]]) looks up and
150 (hopefully) cause the right snippet to be expanded for you.
151
152 ** Setting up =yas-snippet-dirs=
153
154 The emacs variable [[sym:yas-snippet-dirs][=yas-snippet-dirs=]] tells YASnippet
155 which collections to consider. It's used when you activate
156 [[sym:yas-global-mode][=yas-global-mode=]] or call
157 [[sym:yas-reload-all][=yas-reload-all=]] interactively.
158
159 The default considers:
160
161 - a personal collection that lives in =~/.emacs.d/snippets=
162 - the bundled collection, taken as a relative path to =yasnippet.el= localtion
163
164 When you come across other snippet collections, do the following to try them
165 out:
166
167 #+begin_src emacs-lisp :exports code
168 ;; Develop in ~/emacs.d/mysnippets, but also
169 ;; try out snippets in ~/Downloads/interesting-snippets
170 (setq yas-snippet-dirs '("~/emacs.d/mysnippets"
171 "~/Downloads/interesting-snippets"))
172
173 ;; OR, keeping yasnippet's defaults try out ~/Downloads/interesting-snippets
174 (setq yas-snippet-dirs (append yas-snippet-dirs
175 '("~/Downloads/interesting-snippets")))
176 #+end_src
177
178 Collections appearing earlier in the list shadow snippets with same names
179 appearing in collections later in the list. [[sym:yas-new-snippet][=yas-new-snippet=]] always stores
180 snippets in the first collection.
181
182 ** The =.yas-parents= file
183
184 It's very useful to have certain modes share snippets between themselves. To do
185 this, choose a mode subdirectory and place a =.yas-parents= containing a
186 whitespace-separated list of other mode names. When you reload those modes
187 become parents of the original mode.
188
189 #+begin_example
190 .
191 |-- c-mode
192 | |-- .yas-parents # contains "cc-mode text-mode"
193 | `-- printf
194 |-- cc-mode
195 | |-- for
196 | `-- while
197 |-- java-mode
198 | |-- .yas-parents # contains "cc-mode text-mode"
199 | `-- println
200 `-- text-mode
201 |-- email
202 `-- time
203 #+end_example
204
205 ** TODO The =.yas-make-groups= file
206
207 If you place an empty plain text file =.yas-make-groups= inside one of the
208 mode directories, the names of these sub-directories are considered groups of
209 snippets and [[snippet-menu][the menu]] is organized much more cleanly:
210
211 (TODO image)
212
213 Another alternative way to achieve this is to place a =# group:= directive
214 inside the snippet definition. See [[#writing-snippets][Writing Snippets]]
215
216 #+begin_example
217 $ tree ruby-mode/
218 ruby-mode/
219 |-- .yas-make-groups
220 |-- collections
221 | |-- each
222 | `-- ...
223 |-- control structure
224 | |-- forin
225 | `-- ...
226 |-- definitions
227 | `-- ...
228 `-- general
229 `-- ...
230 #+end_example
231
232 Yet another way to create a nice snippet menu is to write into
233 =.yas-make-groups= a menu definition. TODO
234
235 ** TODO The =.yas-setup.el= file
236
237 *** TODO
238
239 ** TODO The =.yas-compiled-snippet.el= file
240
241 *** TODO
242
243 ** The =.yas-skip= file
244
245 * Expanding Snippets
246
247 :PROPERTIES:
248 :CUSTOM_ID: expand-snippets
249 :END:
250
251 This section describes how YASnippet chooses snippets for expansion at point.
252
253 Maybe, you'll want some snippets to be expanded in a particular
254 mode, or only under certain conditions, or be prompted using
255
256 ** Triggering expansion
257
258 To make a snippet expand after the cursor:
259
260 * Type the snippet's *trigger key* then calling [[sym:yas-expand][=yas-expand=]]. It's bound to
261 =TAB= and =<tab>= by default, to change it use
262
263 #+begin_src emacs-lisp :exports code
264 (define-key yas-minor-mode-map (kbd "<tab>") nil)
265 (define-key yas-minor-mode-map (kbd "TAB") nil)
266 (define-key yas-minor-mode-map (kbd "<the new key>") 'yas-expand)
267 #+end_src
268
269 * Use the snippet's *keybinding*.
270
271 * Call [[sym:yas-insert-snippet][=yas-insert-snippet=]] (use =M-x
272 yas-insert-snippet== or its keybinding =C-c & C-s=).
273
274 * By expanding directly from the "YASnippet" menu in the menu-bar
275
276 * Using hippie-expand
277
278 * Use m2m's excellent auto-complete
279
280 # Local Variables:
281 # mode: org
282 # fill-column: 80
283 # coding: utf-8
284 # End: