I'm wondering if I can refer to another snippet within a snippet in VSCode user defined snippet.
Say I have
"Test1": {
"prefix": "snippet_test1",
"body":
"something"
}
and is there a way to insert snippet_test1 in another snippet like
"Test2": {
"prefix": "snippet_test2",
"body":
"${1:snippet_test1}"
}
Now snippet_test2 just outputs snippet_test1 instead of the content of snippet_test1.
-
I don't believe so, but you could chain snippets together with a macro easily.Mark– Mark2019年10月26日 01:53:23 +00:00Commented Oct 26, 2019 at 1:53
-
1do you know how to add a macro to do that?Jin Yan– Jin Yan2019年10月27日 02:19:11 +00:00Commented Oct 27, 2019 at 2:19
-
1I've opened github.com/microsoft/vscode/issues/101526 to VSC repo for exactly this since I have so many useful snippets I can't write because there is no way to embed snippets within other snippets. If this gets 20 thumbs ups, then they will implement it.nichollsg– nichollsg2020年07月07日 18:38:37 +00:00Commented Jul 7, 2020 at 18:38
2 Answers 2
I think the only way to include or nest snippets within each other is to use a macro or some other programmatic way. Here is a solution using the macro extension multi-command.
Lets say you have these three snippets (in some snippets file):
"Master Snippet": {
"prefix": "master_snippet",
"body": [
"body of master",
"snippet2 = 2ドル",
"1ドル",
"some other stuff",
"1ドル",
],
"description": "build the multi-snippet"
},
"snippet1": {
"prefix": "sn1",
"body": [
"body of snippet1",
],
"description": "insert1"
},
"snippet2": {
"prefix": "sn2",
"body": [
"I am snippet2",
],
"description": "insert2"
},
Then your macro would print Master Snippet first and then wherever the cursor is - the cursor will be in both 1ドル tabstop positions initally - the macro will insert the snippet1.
Then with the "jumpToNextSnippetPlaceholder", command in the macro you will jump to the next tabstop 2ドル which could be anywhere - I put it before 1ドル (where snippet1 got inserted) and snippet2 will be inserted at tabstop 2ドル.
You can see that the Master Snippet is where you build the structure for inserting the other snippets - according to tabstops.
The macro would look like this (in your settings.json):
"multiCommand.commands": [
{
"command": "multiCommand.insertMultipleSnippets",
"sequence": [
{
"command": "editor.action.insertSnippet",
"args": {
"name": "Master Snippet",
}
},
{
"command": "editor.action.insertSnippet",
"args": {
"name": "snippet1",
}
},
"jumpToNextSnippetPlaceholder",
{
"command": "editor.action.insertSnippet",
"args": {
"name": "snippet2",
}
},
]
}
],
and then trigger the macro with some keybinding (keybindings.json):
{
"key": "alt+m", // or whichever keybinding you choose
"command": "extension.multiCommand.execute",
"args": { "command": "multiCommand.insertMultipleSnippets" },
"when": "editorTextFocus"
},
You cannot use any snippet prefix to trigger the whole macro, but you can still use the individual snippet prefixes to trigger each snippet individually if you wish.
With the above Master Snippet, snippet1 and snippet2 the result of running the macro would be:
body of master snippet
snippet2 = I am snippet2
body of snippet1
some other stuff
body of snippet1
You do lose some functionality, like the inserted snippet cannot be pre-selected like placeholder text would be - if used like ${1:howdy}, the placeholder text howdy is just overwritten by the first snippet inserted.
Comments
@Mark provides a good answer to use macros and I got another possible answer for who are interested.
"Test1": {
"prefix": "snippet_test1",
"body":
"something"
}
"Test2": {
"prefix": "snippet_test2",
"body":
"${1:snippet_test1}"
}
For Test2, it does only show snippet_test1 other than Test1 content but if you hit ctrl+space at snippet_test1, it will show a list of possible snippet available and you can expand text in snippet_test2 to the full content in snippet_test1.
1 Comment
snippet_test2 flow if user inserts other snippet and you cant move to next tab stop on test2. I'm not sure if the macro version works properly either.Explore related questions
See similar questions with these tags.