@@ -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. |
@@ -119,6 +118,14 @@ automatically by Sublime Text.
119118| ` $TM_SOFT_TABS ` | ` YES ` if ` translate_tabs_to_spaces ` is true, otherwise ` NO ` . |
120119| ` $TM_TAB_SIZE ` | Spaces per-tab (controlled by the ` tab_size ` option). |
121120
121+
122+ ::: tip Note
123+ Packages can define more, environment variables via [ Shell Variables in Metafiles] [ shell-vars ] .
124+
125+ [ shell-vars ] : ../../reference/metadata#shell-variables
126+ :::
127+
128+
122129Let's see a simple example of a snippet using variables:
123130
124131``` perl
@@ -295,3 +302,90 @@ Transformation: ${TM_FILENAME/(\w+)\.js/\1/g}
295302
296303Transformation: MyModule
297304```
305+
306+ ### Passing custom arguments
307+
308+ Command Palette items, key bindings and plugins
309+ can pass values
310+ of fields (` $1 ` or ` ${1:placeholder} ` )
311+ or custom arguments (` $any_variable ` )
312+ to snippets,
313+ when inserting them via [ insert_snippet] [ ] command.
314+
315+ The following examples insert ` "Hello World!" ` to buffer.
316+
317+ Let's assume a generic snippet _ Packages/User/My Snippet.sublime-snippet_
318+ which provides a field ` $1 `
319+ and expects a custom argument ` $subject ` :
320+
321+ ``` xml
322+ <snippet >
323+ <content ><![CDATA[ $1 $subject!]]> </content >
324+ <description >Snippet with 2 fields</description >
325+ </snippet >
326+ ```
327+
328+ It can be triggered by key bindings with static values being passed.
329+
330+ ``` json
331+ [
332+ {
333+ "keys" : [" ctrl+k" , " ctrl+w" ],
334+ "command" : " insert_snippet" ,
335+ "args" : {
336+ "name" : " Packages/User/My Snippet.sublime-snippet" ,
337+ "1" : " Hello" ,
338+ "subject" : " World"
339+ }
340+ }
341+ ]
342+ ```
343+
344+ Plugins can pass dynamically determined values.
345+
346+ ``` py
347+ import sublime_plugin
348+
349+ class MyInsertSnippetCommand (sublime_plugin .TextCommand ):
350+ def run (self , edit ):
351+ field1 = " Hello"
352+
353+ self .view.run_command(
354+ cmd = " insert_snippet" ,
355+ args = {
356+ " name" : " Packages/User/My Snippet.sublime-snippet" ,
357+ " 1" : field1,
358+ " subject" : self .get_subject()
359+ }
360+ )
361+
362+ def get_subject (self ):
363+ return " World"
364+ ```
365+
366+ Field values and arguments can also be passed to inline snippet content.
367+
368+ ``` py
369+ import sublime_plugin
370+
371+ class MyInsertSnippetCommand (sublime_plugin .TextCommand ):
372+ def run (self , edit ):
373+
374+ snippet_content = " $1 $subject!"
375+
376+ field1 = " Hello"
377+
378+ self .view.run_command(
379+ cmd = " insert_snippet" ,
380+ args = {
381+ " contents" : snippet_content,
382+ " 1" : field1,
383+ " subject" : self .get_subject()
384+ }
385+ )
386+
387+ def get_subject (self ):
388+ return " World"
389+ ```
390+
391+ [ insert_snippet ] : ../../reference/commands#insert_snippet
0 commit comments