Creating Visual Studio Code Snippet

I would like to create a VSC snippet that types ‘console.log()’.

I have got it to work with this snippet:

“Print to console”: {
“prefix”: “log”,
“scope”: “(javascript, typescript)”,
“body”: [
“console.log(’$1’);”,
“$2”
],
“description”: “Log output to console”
}

I would like to change the prefix to “cl”. So far that doesn’t work

I was not familiar with snippets in VS Code but I looked at the documentation Snippets in Visual Studio Code. It says snippets are templates that make it easier to enter repeating code patterns. I also looked at the User Snippets for my VS Code. I do not have any but it does show an example just like your except you have added scope.

I do not understand what you mean by types. Perhaps there is a miscommunication by language. In that example, console.log(’$1’); is inserted into the JavaScript or TypeScript, not typed. I don’t want to be critical but I want to ensure we are communicating.

Note that your scope has parentheses. Look at the documentation/samples.

The documentation is unclear about how to name global snippets but evidently it is not very specific. Assuming the file name is correct, I think the following works for me.

{
"Print to console": {
	"scope": "javascript,typescript",
	"prefix": ["log","cl"],
	"body": [
	"console.log('$1');",
	"$2"
	],
	"description": "Log output to console"
	}
}

Yes, ‘insert’ is the correct term. I copied your code. ‘log’ works but ‘cl’ doesn’t. To be more specific, when I type ‘log’, a short list pops up. One of the items on the list is ‘log’ with a square to its left. Choosing that item results in the correct insertion of ‘console.log();’. But if I type ‘cl’, a longer list pops up. However, there is no ‘cl’ with a square to its left to choose. Thanks.

I got it. There was some kind of syntax issue involving braces. I think it had to do with the comment code at top of screen. This worked:

{ “Print to console”: {
“scope”: “javascript,typescript”,
“prefix”: [“cl”],
“body”: [
“console.log(‘$1’);”,
“$2”
],
“description”: “Log output to console”
}
}

On a related issue, while playing with this I created 2 extra snippet files. How do I delete them? Thanks.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.