]> git.r.bdr.sh - rbdr/dotfiles/blame_incremental - vim/doc/snipMate.txt
Move EasyMotion and LargeFile to pathogen bundle
[rbdr/dotfiles] / vim / doc / snipMate.txt
... / ...
CommitLineData
1*snipMate.txt* Plugin for using TextMate-style snippets in Vim.
2
3snipMate *snippet* *snippets* *snipMate*
4Last 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
15For Vim version 7.0 or later.
16This plugin only works if 'compatible' is not set.
17{Vi does not have any of these features.}
18
19==============================================================================
20DESCRIPTION *snipMate-description*
21
22snipMate.vim implements some of TextMate's snippets features in Vim. A
23snippet is a piece of often-typed text that you can insert into your
24document using a trigger word followed by a <tab>.
25
26For instance, in a C file using the default installation of snipMate.vim, if
27you 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
34To go to the next item in the loop, simply <tab> over to it; if there is
35repeated code, such as the "i" variable in this example, you can simply
36start typing once it's highlighted and all the matches specified in the
37snippet will be updated. To go in reverse, use <shift-tab>.
38
39==============================================================================
40SYNTAX *snippet-syntax*
41
42Snippets can be defined in two ways. They can be in their own file, named
43after their trigger in 'snippets/<filetype>/<trigger>.snippet', or they can be
44defined 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
49to activate snippets for both HTML and eRuby for the current file.
50
51The syntax for snippets in *.snippets files is the following: >
52
53 snippet trigger
54 expanded text
55 more expanded text
56
57Note that the first hard tab after the snippet trigger is required, and not
58expanded in the actual snippet. The syntax for *.snippet files is the same,
59only without the trigger declaration and starting indentation.
60
61Also note that snippets must be defined using hard tabs. They can be expanded
62to spaces later if desired (see |snipMate-indenting|).
63
64"#" is used as a line-comment character in *.snippets files; however, they can
65only 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<
74This should hopefully be obvious with the included syntax highlighting.
75
76 *snipMate-${#}*
77Tab stops ~
78
79By default, the cursor is placed at the end of a snippet. To specify where the
80cursor is to be placed next, use "${#}", where the # is the number of the tab
81stop. E.g., to place the cursor first on the id of a <div> tag, and then allow
82the 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-$#*
90Placeholders ~
91
92Placeholder text can be supplied using "${#:text}", where # is the number of
93the tab stop. This text then can be copied throughout the snippet using "$#",
94given # 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
101This will cause "count" to first be selected and change if the user starts
102typing. When <tab> is pressed, the "i" in ${2}'s position will be selected;
103all $2 variables will default to "i" and automatically be updated if the user
104starts typing.
105NOTE: "$#" syntax is used only for variables, not for tab stops as in TextMate.
106
107Variables within variables are also possible. For instance: >
108
109 snippet opt
110 <option value="${1:option}">${2:$1}</option>
111
112Will, as usual, cause "option" to first be selected and update all the $1
113variables 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,
115allowing the user to change it if he wishes.
116
117To copy a value throughout a snippet without supplying default text, simply
118use the "${#:}" construct without the text; e.g.: >
119
120 snippet foo
121 ${1:}bar$1
122< *snipMate-commands*
123Interpolated Vim Script ~
124
125Snippets can also contain Vim script commands that are executed (via |eval()|)
126when the snippet is inserted. Commands are given inside backticks (`...`); for
127TextMates's functionality, use the |system()| function. E.g.: >
128
129 snippet date
130 `system("date +%Y-%m-%d")`
131
132will insert the current date, assuming you are on a Unix system. Note that you
133can also (and should) use |strftime()| for this example.
134
135Filename([{expr}] [, {defaultText}]) *snipMate-filename* *Filename()*
136
137Since the current filename is used often in snippets, a default function
138has been defined for it in snipMate.vim, appropriately called Filename().
139
140With no arguments, the default filename without an extension is returned;
141the first argument specifies what to place before or after the filename,
142and the second argument supplies the default text to be used if the file
143has not been named. "$1" in the first argument is replaced with the filename;
144if you only want the filename to be returned, the first argument can be left
145blank. Examples: >
146
147 snippet filename
148 `Filename()`
149 snippet filename_with_default
150 `Filename('', 'name')`
151 snippet filename_foo
152 `filename('$1_foo')`
153
154The first example returns the filename if it the file has been named, and an
155empty string if it hasn't. The second returns the filename if it's been named,
156and "name" if it hasn't. The third returns the filename followed by "_foo" if
157it has been named, and an empty string if it hasn't.
158
159 *multi_snip*
160To specify that a snippet can have multiple matches in a *.snippets file, use
161this 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
168In this example, when "trigger<tab>" is typed, a numbered menu containing all
169of the descriptions of the "trigger" will be shown; when the user presses the
170corresponding number, that snippet will then be expanded.
171
172To create a snippet with multiple matches using *.snippet files,
173simply place all the snippets in a subdirectory with the trigger name:
174'snippets/<filetype>/<trigger>/<name>.snippet'.
175
176==============================================================================
177USAGE *snipMate-usage*
178
179 *'snippets'* *g:snippets_dir*
180Snippets 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
183one, change the g:snippets_dir variable in your |.vimrc| to your preferred
184directory, or use the |ExtractSnips()|function. This will be used by the
185|globpath()| function, and so accepts the same syntax as it (e.g.,
186comma-separated paths).
187
188ExtractSnipsFile({directory}, {filetype}) *ExtractSnipsFile()* *.snippets*
189
190ExtractSnipsFile() extracts the specified *.snippets file for the given
191filetype. A .snippets file contains multiple snippet declarations for the
192filetype. It is further explained above, in |snippet-syntax|.
193
194ExtractSnips({directory}, {filetype}) *ExtractSnips()* *.snippet*
195
196ExtractSnips() extracts *.snippet files from the specified directory and
197defines them as snippets for the given filetype. The directory tree should
198look like this: 'snippets/<filetype>/<trigger>.snippet'. If the snippet has
199multiple matches, it should look like this:
200'snippets/<filetype>/<trigger>/<name>.snippet' (see |multi_snip|).
201
202ResetAllSnippets() *ResetAllSnippets()*
203ResetAllSnippets() removes all snippets from memory. This is useful to put at
204the top of a snippet setup file for if you would like to |:source| it multiple
205times.
206
207ResetSnippets({filetype}) *ResetSnippets()*
208ResetSnippets() removes all snippets from memory for the given filetype.
209
210ReloadAllSnippets() *ReloadAllSnippets()*
211ReloadAllSnippets() reloads all snippets for all filetypes. This is useful for
212testing and debugging.
213
214ReloadSnippets({filetype}) *ReloadSnippets()*
215ReloadSnippets() reloads all snippets for the given filetype.
216
217 *list-snippets* *i_CTRL-R_<Tab>*
218If you would like to see what snippets are available, simply type <c-r><tab>
219in the current buffer to show a list via |popupmenu-completion|.
220
221==============================================================================
222SETTINGS *snipMate-settings* *g:snips_author*
223
224The g:snips_author string (similar to $TM_FULLNAME in TextMate) should be set
225to 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*
232If you would like your snippets to be expanded using spaces instead of tabs,
233just enable 'expandtab' and set 'softtabstop' to your preferred amount of
234spaces. If 'softtabstop' is not set, 'shiftwidth' is used instead.
235
236 *snipMate-remap*
237snipMate does not come with a setting to customize the trigger key, but you
238can remap it easily in the two lines it's defined in the 'after' directory
239under 'plugin/snipMate.vim'. For instance, to change the trigger key
240to CTRL-J, just change this: >
241
242 ino <tab> <c-r>=TriggerSnippet()<cr>
243 snor <tab> <esc>i<right><c-r>=TriggerSnippet()<cr>
244
245to this: >
246 ino <c-j> <c-r>=TriggerSnippet()<cr>
247 snor <c-j> <esc>i<right><c-r>=TriggerSnippet()<cr>
248
249==============================================================================
250FEATURES *snipMate-features*
251
252snipMate.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==============================================================================
268DISADVANTAGES *snipMate-disadvantages*
269
270snipMate.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
285Perhaps some of these features will be added in a later release.
286
287==============================================================================
288CONTACT *snipMate-contact* *snipMate-author*
289
290To contact the author (Michael Sanders), please email:
291 msanders42+snipmate <at> gmail <dot> com
292
293I greatly appreciate any suggestions or improvements offered for the script.
294
295==============================================================================
296LICENSE *snipMate-license*
297
298snipMate is released under the MIT license:
299
300Copyright 2009-2010 Michael Sanders. All rights reserved.
301
302Permission is hereby granted, free of charge, to any person obtaining a copy
303of this software and associated documentation files (the "Software"), to deal
304in the Software without restriction, including without limitation the rights
305to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
306copies of the Software, and to permit persons to whom the Software is
307furnished to do so, subject to the following conditions:
308
309The above copyright notice and this permission notice shall be included in all
310copies or substantial portions of the Software.
311
312The software is provided "as is", without warranty of any kind, express or
313implied, including but not limited to the warranties of merchantability,
314fitness for a particular purpose and noninfringement. In no event shall the
315authors or copyright holders be liable for any claim, damages or other
316liability, whether in an action of contract, tort or otherwise, arising from,
317out of or in connection with the software or the use or other dealings in the
318software.
319
320==============================================================================
321
322vim:tw=78:ts=8:ft=help:norl: