Skip to content

Commit cc68aaa

Browse files
committed
Describe passing custom arguments to snippets
This commit adds description how to pass field or custom argument values to snippets using some small code examples. caused by https://forum.sublimetext.com/t/how-do-i-insert-a-snippet-with-paramn-variables-from-a-plugin/76200
1 parent 02b2839 commit cc68aaa

File tree

2 files changed

+90
-2
lines changed

2 files changed

+90
-2
lines changed

docs/guide/extensibility/snippets.md

Lines changed: 88 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,12 @@ element's text in examples unless otherwise noted.
9898
### Environment Variables
9999

100100
Snippets 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
102102
automatically 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

296295
Transformation: 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

docs/reference/commands.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,8 @@ Inserts a snippet from a string or *.sublime-snippet* file.
331331
backslashes `\` have to be escaped, like in every other JSON string.
332332
- **name** (String): [Relative path][About Paths in Command Arguments] to the
333333
*.sublime-snippet* file to be inserted.
334+
- **args** (String): a list of key-value pairs (e.g.: `"myparam": "value"`)
335+
specifying values to replace arbitrary variables (e.g.: `$myparam`) with.
334336

335337
::: info See Also
336338
[Snippets][]

0 commit comments

Comments
 (0)