@@ -98,13 +98,12 @@ element's text in examples unless otherwise noted.
9898### Environment Variables
9999
100100Snippets have access to contextual information in the form of
101- environment variables. The values of the variables listed below are set
101+ environment variables. The values of variables listed below are set
102102automatically by Sublime Text.
103103
104104
105105| Variable | Description |
106106| -------------------- | --------------------------------------------------------------------- |
107- | ` $PARAM1 .. $PARAMn ` | Arguments passed to the ` insert_snippet ` command. (Not covered here.) |
108107| ` $SELECTION ` | The text that was selected when the snippet was triggered. |
109108| ` $TM_CURRENT_LINE ` | Content of the cursor's line when the snippet was triggered. |
110109| ` $TM_CURRENT_WORD ` | Word under the cursor when the snippet was triggered. |
@@ -295,3 +294,90 @@ Transformation: ${TM_FILENAME/(\w+)\.js/\1/g}
295294
296295Transformation: MyModule
297296```
297+
298+ ### Passing custom arguments
299+
300+ Command Palette items, key bindings and plugins
301+ can pass values
302+ of fields (` $1 ` or ` ${1:placeholder} ` )
303+ or custom arguments (` $any_variable ` )
304+ to snippets,
305+ when inserting them via [ insert_snippet] [ ] command.
306+
307+ The following examples insert ` "Hello World!" ` to buffer.
308+
309+ Let's assume a generic snippet _ Packages/User/My Snippet.sublime-snippet_
310+ which provides a field ` $1 `
311+ and expects a custom argument ` $subject ` :
312+
313+ ``` xml
314+ <snippet >
315+ <content ><![CDATA[ $1 $subject!]]> </content >
316+ <description >Snippet with 2 fields</description >
317+ </snippet >
318+ ```
319+
320+ It can be triggered by key bindings with static values being passed.
321+
322+ ``` json
323+ [
324+ {
325+ "keys" : [" ctrl+k" , " ctrl+w" ],
326+ "command" : " insert_snippet" ,
327+ "args" : {
328+ "name" : " Packages/User/My Snippet.sublime-snippet" ,
329+ "1" : " Hello" ,
330+ "subject" : " World"
331+ }
332+ }
333+ ]
334+ ```
335+
336+ Plugins can pass dynamically determined values.
337+
338+ ``` py
339+ import sublime_plugin
340+
341+ class MyInsertSnippetCommand (sublime_plugin .TextCommand ):
342+ def run (self , edit ):
343+ field1 = " Hello"
344+
345+ self .view.run_command(
346+ cmd = " insert_snippet" ,
347+ args = {
348+ " name" : " Packages/User/My Snippet.sublime-snippet" ,
349+ " 1" : field1,
350+ " subject" : self .get_subject()
351+ }
352+ )
353+
354+ def get_subject (self ):
355+ return " World"
356+ ```
357+
358+ Field values and arguments can also be passed to inline snippet content.
359+
360+ ``` py
361+ import sublime_plugin
362+
363+ class MyInsertSnippetCommand (sublime_plugin .TextCommand ):
364+ def run (self , edit ):
365+
366+ snippet_content = " $1 $subject!"
367+
368+ field1 = " Hello"
369+
370+ self .view.run_command(
371+ cmd = " insert_snippet" ,
372+ args = {
373+ " contents" : snippet_content,
374+ " 1" : field1,
375+ " subject" : self .get_subject()
376+ }
377+ )
378+
379+ def get_subject (self ):
380+ return " World"
381+ ```
382+
383+ [ insert_snippet ] : ../../reference/commands#insert_snippet
0 commit comments