]>
Commit | Line | Data |
---|---|---|
1 | *snipMate.txt* Plugin for using TextMate-style snippets in Vim. | |
2 | ||
3 | snipMate *snippet* *snippets* *snipMate* | |
4 | Last Change: December 27, 2009 | |
5 | ||
6 | |snipMate-description| Description | |
7 | |snipMate-syntax| Snippet syntax | |
8 | |snipMate-usage| Usage | |
9 | |snipMate-settings| Settings | |
10 | |snipMate-features| Features | |
11 | |snipMate-disadvantages| Disadvantages to TextMate | |
12 | |snipMate-contact| Contact | |
13 | |snipMate-license| License | |
14 | ||
15 | For Vim version 7.0 or later. | |
16 | This plugin only works if 'compatible' is not set. | |
17 | {Vi does not have any of these features.} | |
18 | ||
19 | ============================================================================== | |
20 | DESCRIPTION *snipMate-description* | |
21 | ||
22 | snipMate.vim implements some of TextMate's snippets features in Vim. A | |
23 | snippet is a piece of often-typed text that you can insert into your | |
24 | document using a trigger word followed by a <tab>. | |
25 | ||
26 | For instance, in a C file using the default installation of snipMate.vim, if | |
27 | you type "for<tab>" in insert mode, it will expand a typical for loop in C: > | |
28 | ||
29 | for (i = 0; i < count; i++) { | |
30 | ||
31 | } | |
32 | ||
33 | ||
34 | To go to the next item in the loop, simply <tab> over to it; if there is | |
35 | repeated code, such as the "i" variable in this example, you can simply | |
36 | start typing once it's highlighted and all the matches specified in the | |
37 | snippet will be updated. To go in reverse, use <shift-tab>. | |
38 | ||
39 | ============================================================================== | |
40 | SYNTAX *snippet-syntax* | |
41 | ||
42 | Snippets can be defined in two ways. They can be in their own file, named | |
43 | after their trigger in 'snippets/<filetype>/<trigger>.snippet', or they can be | |
44 | defined together in a 'snippets/<filetype>.snippets' file. Note that dotted | |
45 | 'filetype' syntax is supported -- e.g., you can use > | |
46 | ||
47 | :set ft=html.eruby | |
48 | ||
49 | to activate snippets for both HTML and eRuby for the current file. | |
50 | ||
51 | The syntax for snippets in *.snippets files is the following: > | |
52 | ||
53 | snippet trigger | |
54 | expanded text | |
55 | more expanded text | |
56 | ||
57 | Note that the first hard tab after the snippet trigger is required, and not | |
58 | expanded in the actual snippet. The syntax for *.snippet files is the same, | |
59 | only without the trigger declaration and starting indentation. | |
60 | ||
61 | Also note that snippets must be defined using hard tabs. They can be expanded | |
62 | to spaces later if desired (see |snipMate-indenting|). | |
63 | ||
64 | "#" is used as a line-comment character in *.snippets files; however, they can | |
65 | only be used outside of a snippet declaration. E.g.: > | |
66 | ||
67 | # this is a correct comment | |
68 | snippet trigger | |
69 | expanded text | |
70 | snippet another_trigger | |
71 | # this isn't a comment! | |
72 | expanded text | |
73 | < | |
74 | This should hopefully be obvious with the included syntax highlighting. | |
75 | ||
76 | *snipMate-${#}* | |
77 | Tab stops ~ | |
78 | ||
79 | By default, the cursor is placed at the end of a snippet. To specify where the | |
80 | cursor is to be placed next, use "${#}", where the # is the number of the tab | |
81 | stop. E.g., to place the cursor first on the id of a <div> tag, and then allow | |
82 | the user to press <tab> to go to the middle of it: | |
83 | > | |
84 | snippet div | |
85 | <div id="${1}"> | |
86 | ${2} | |
87 | </div> | |
88 | < | |
89 | *snipMate-placeholders* *snipMate-${#:}* *snipMate-$#* | |
90 | Placeholders ~ | |
91 | ||
92 | Placeholder text can be supplied using "${#:text}", where # is the number of | |
93 | the tab stop. This text then can be copied throughout the snippet using "$#", | |
94 | given # is the same number as used before. So, to make a C for loop: > | |
95 | ||
96 | snippet for | |
97 | for (${2:i}; $2 < ${1:count}; $1++) { | |
98 | ${4} | |
99 | } | |
100 | ||
101 | This will cause "count" to first be selected and change if the user starts | |
102 | typing. When <tab> is pressed, the "i" in ${2}'s position will be selected; | |
103 | all $2 variables will default to "i" and automatically be updated if the user | |
104 | starts typing. | |
105 | NOTE: "$#" syntax is used only for variables, not for tab stops as in TextMate. | |
106 | ||
107 | Variables within variables are also possible. For instance: > | |
108 | ||
109 | snippet opt | |
110 | <option value="${1:option}">${2:$1}</option> | |
111 | ||
112 | Will, as usual, cause "option" to first be selected and update all the $1 | |
113 | variables if the user starts typing. Since one of these variables is inside of | |
114 | ${2}, this text will then be used as a placeholder for the next tab stop, | |
115 | allowing the user to change it if he wishes. | |
116 | ||
117 | To copy a value throughout a snippet without supplying default text, simply | |
118 | use the "${#:}" construct without the text; e.g.: > | |
119 | ||
120 | snippet foo | |
121 | ${1:}bar$1 | |
122 | < *snipMate-commands* | |
123 | Interpolated Vim Script ~ | |
124 | ||
125 | Snippets can also contain Vim script commands that are executed (via |eval()|) | |
126 | when the snippet is inserted. Commands are given inside backticks (`...`); for | |
127 | TextMates's functionality, use the |system()| function. E.g.: > | |
128 | ||
129 | snippet date | |
130 | `system("date +%Y-%m-%d")` | |
131 | ||
132 | will insert the current date, assuming you are on a Unix system. Note that you | |
133 | can also (and should) use |strftime()| for this example. | |
134 | ||
135 | Filename([{expr}] [, {defaultText}]) *snipMate-filename* *Filename()* | |
136 | ||
137 | Since the current filename is used often in snippets, a default function | |
138 | has been defined for it in snipMate.vim, appropriately called Filename(). | |
139 | ||
140 | With no arguments, the default filename without an extension is returned; | |
141 | the first argument specifies what to place before or after the filename, | |
142 | and the second argument supplies the default text to be used if the file | |
143 | has not been named. "$1" in the first argument is replaced with the filename; | |
144 | if you only want the filename to be returned, the first argument can be left | |
145 | blank. Examples: > | |
146 | ||
147 | snippet filename | |
148 | `Filename()` | |
149 | snippet filename_with_default | |
150 | `Filename('', 'name')` | |
151 | snippet filename_foo | |
152 | `filename('$1_foo')` | |
153 | ||
154 | The first example returns the filename if it the file has been named, and an | |
155 | empty string if it hasn't. The second returns the filename if it's been named, | |
156 | and "name" if it hasn't. The third returns the filename followed by "_foo" if | |
157 | it has been named, and an empty string if it hasn't. | |
158 | ||
159 | *multi_snip* | |
160 | To specify that a snippet can have multiple matches in a *.snippets file, use | |
161 | this syntax: > | |
162 | ||
163 | snippet trigger A description of snippet #1 | |
164 | expand this text | |
165 | snippet trigger A description of snippet #2 | |
166 | expand THIS text! | |
167 | ||
168 | In this example, when "trigger<tab>" is typed, a numbered menu containing all | |
169 | of the descriptions of the "trigger" will be shown; when the user presses the | |
170 | corresponding number, that snippet will then be expanded. | |
171 | ||
172 | To create a snippet with multiple matches using *.snippet files, | |
173 | simply place all the snippets in a subdirectory with the trigger name: | |
174 | 'snippets/<filetype>/<trigger>/<name>.snippet'. | |
175 | ||
176 | ============================================================================== | |
177 | USAGE *snipMate-usage* | |
178 | ||
179 | *'snippets'* *g:snippets_dir* | |
180 | Snippets are by default looked for any 'snippets' directory in your | |
181 | 'runtimepath'. Typically, it is located at '~/.vim/snippets/' on *nix or | |
182 | '$HOME\vimfiles\snippets\' on Windows. To change that location or add another | |
183 | one, change the g:snippets_dir variable in your |.vimrc| to your preferred | |
184 | directory, or use the |ExtractSnips()|function. This will be used by the | |
185 | |globpath()| function, and so accepts the same syntax as it (e.g., | |
186 | comma-separated paths). | |
187 | ||
188 | ExtractSnipsFile({directory}, {filetype}) *ExtractSnipsFile()* *.snippets* | |
189 | ||
190 | ExtractSnipsFile() extracts the specified *.snippets file for the given | |
191 | filetype. A .snippets file contains multiple snippet declarations for the | |
192 | filetype. It is further explained above, in |snippet-syntax|. | |
193 | ||
194 | ExtractSnips({directory}, {filetype}) *ExtractSnips()* *.snippet* | |
195 | ||
196 | ExtractSnips() extracts *.snippet files from the specified directory and | |
197 | defines them as snippets for the given filetype. The directory tree should | |
198 | look like this: 'snippets/<filetype>/<trigger>.snippet'. If the snippet has | |
199 | multiple matches, it should look like this: | |
200 | 'snippets/<filetype>/<trigger>/<name>.snippet' (see |multi_snip|). | |
201 | ||
202 | ResetAllSnippets() *ResetAllSnippets()* | |
203 | ResetAllSnippets() removes all snippets from memory. This is useful to put at | |
204 | the top of a snippet setup file for if you would like to |:source| it multiple | |
205 | times. | |
206 | ||
207 | ResetSnippets({filetype}) *ResetSnippets()* | |
208 | ResetSnippets() removes all snippets from memory for the given filetype. | |
209 | ||
210 | ReloadAllSnippets() *ReloadAllSnippets()* | |
211 | ReloadAllSnippets() reloads all snippets for all filetypes. This is useful for | |
212 | testing and debugging. | |
213 | ||
214 | ReloadSnippets({filetype}) *ReloadSnippets()* | |
215 | ReloadSnippets() reloads all snippets for the given filetype. | |
216 | ||
217 | *list-snippets* *i_CTRL-R_<Tab>* | |
218 | If you would like to see what snippets are available, simply type <c-r><tab> | |
219 | in the current buffer to show a list via |popupmenu-completion|. | |
220 | ||
221 | ============================================================================== | |
222 | SETTINGS *snipMate-settings* *g:snips_author* | |
223 | ||
224 | The g:snips_author string (similar to $TM_FULLNAME in TextMate) should be set | |
225 | to your name; it can then be used in snippets to automatically add it. E.g.: > | |
226 | ||
227 | let g:snips_author = 'Hubert Farnsworth' | |
228 | snippet name | |
229 | `g:snips_author` | |
230 | < | |
231 | *snipMate-expandtab* *snipMate-indenting* | |
232 | If you would like your snippets to be expanded using spaces instead of tabs, | |
233 | just enable 'expandtab' and set 'softtabstop' to your preferred amount of | |
234 | spaces. If 'softtabstop' is not set, 'shiftwidth' is used instead. | |
235 | ||
236 | *snipMate-remap* | |
237 | snipMate does not come with a setting to customize the trigger key, but you | |
238 | can remap it easily in the two lines it's defined in the 'after' directory | |
239 | under 'plugin/snipMate.vim'. For instance, to change the trigger key | |
240 | to CTRL-J, just change this: > | |
241 | ||
242 | ino <tab> <c-r>=TriggerSnippet()<cr> | |
243 | snor <tab> <esc>i<right><c-r>=TriggerSnippet()<cr> | |
244 | ||
245 | to this: > | |
246 | ino <c-j> <c-r>=TriggerSnippet()<cr> | |
247 | snor <c-j> <esc>i<right><c-r>=TriggerSnippet()<cr> | |
248 | ||
249 | ============================================================================== | |
250 | FEATURES *snipMate-features* | |
251 | ||
252 | snipMate.vim has the following features among others: | |
253 | - The syntax of snippets is very similar to TextMate's, allowing | |
254 | easy conversion. | |
255 | - The position of the snippet is kept transparently (i.e. it does not use | |
256 | markers/placeholders written to the buffer), which allows you to escape | |
257 | out of an incomplete snippet, something particularly useful in Vim. | |
258 | - Variables in snippets are updated as-you-type. | |
259 | - Snippets can have multiple matches. | |
260 | - Snippets can be out of order. For instance, in a do...while loop, the | |
261 | condition can be added before the code. | |
262 | - [New] File-based snippets are supported. | |
263 | - [New] Triggers after non-word delimiters are expanded, e.g. "foo" | |
264 | in "bar.foo". | |
265 | - [New] <shift-tab> can now be used to jump tab stops in reverse order. | |
266 | ||
267 | ============================================================================== | |
268 | DISADVANTAGES *snipMate-disadvantages* | |
269 | ||
270 | snipMate.vim currently has the following disadvantages to TextMate's snippets: | |
271 | - There is no $0; the order of tab stops must be explicitly stated. | |
272 | - Placeholders within placeholders are not possible. E.g.: > | |
273 | ||
274 | '<div${1: id="${2:some_id}}">${3}</div>' | |
275 | < | |
276 | In TextMate this would first highlight ' id="some_id"', and if | |
277 | you hit delete it would automatically skip ${2} and go to ${3} | |
278 | on the next <tab>, but if you didn't delete it it would highlight | |
279 | "some_id" first. You cannot do this in snipMate.vim. | |
280 | - Regex cannot be performed on variables, such as "${1/.*/\U&}" | |
281 | - Placeholders cannot span multiple lines. | |
282 | - Activating snippets in different scopes of the same file is | |
283 | not possible. | |
284 | ||
285 | Perhaps some of these features will be added in a later release. | |
286 | ||
287 | ============================================================================== | |
288 | CONTACT *snipMate-contact* *snipMate-author* | |
289 | ||
290 | To contact the author (Michael Sanders), please email: | |
291 | msanders42+snipmate <at> gmail <dot> com | |
292 | ||
293 | I greatly appreciate any suggestions or improvements offered for the script. | |
294 | ||
295 | ============================================================================== | |
296 | LICENSE *snipMate-license* | |
297 | ||
298 | snipMate is released under the MIT license: | |
299 | ||
300 | Copyright 2009-2010 Michael Sanders. All rights reserved. | |
301 | ||
302 | Permission is hereby granted, free of charge, to any person obtaining a copy | |
303 | of this software and associated documentation files (the "Software"), to deal | |
304 | in the Software without restriction, including without limitation the rights | |
305 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
306 | copies of the Software, and to permit persons to whom the Software is | |
307 | furnished to do so, subject to the following conditions: | |
308 | ||
309 | The above copyright notice and this permission notice shall be included in all | |
310 | copies or substantial portions of the Software. | |
311 | ||
312 | The software is provided "as is", without warranty of any kind, express or | |
313 | implied, including but not limited to the warranties of merchantability, | |
314 | fitness for a particular purpose and noninfringement. In no event shall the | |
315 | authors or copyright holders be liable for any claim, damages or other | |
316 | liability, whether in an action of contract, tort or otherwise, arising from, | |
317 | out of or in connection with the software or the use or other dealings in the | |
318 | software. | |
319 | ||
320 | ============================================================================== | |
321 | ||
322 | vim:tw=78:ts=8:ft=help:norl: |