Profit of Vscode Snippets
Satya Komatineni
Discover what I do not know. Work Ceaselessly in its pursuit.Programming, Curiosity, and Kindness is enough! Inspire others, if not enable.
Say you are writing a markdown file like this for instance.
I want my heading to look like this so that it is clearly separated from the rest
<!-- ********************* -->
# Profit of VSCode Snippets
<!-- ********************* -->
Or I want a code snippet for PowerShell that automatically inserts for me on cue the following
```powershell
code
```
How to see what snippets you already have
ctrl-shift-p
snippets: insert snippet
This will display the existing snippets.
These include what you defined and also the built-in snippets for the type of file you have opened.
How to create a snippet(s) of your own
ctrl-shift-p and "snippets"
use "configure user snippets"
Few notes:
As a result of this the named snippet file is created and opened in the editor.
This file looks like the following
{
// Place your LearnPowershellRepo workspace snippets here. Each snippet is defined under a snippet name and has a scope, prefix, body and
// description. Add comma separated ids of the languages where the snippet is applicable in the scope field. If scope
// is left empty or omitted, the snippet gets applied to all languages. The prefix is what is
// used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders.
// Placeholders with the same ids are connected.
// Example:
// "Print to console": {
// "scope": "javascript,typescript",
// "prefix": "log",
// "body": [
// "console.log('$1');",
// "$2"
// ],
// "description": "Log output to console"
// }
}
Here are two snippets I have added to this snippet file
{
// Place your LearnPowershellRepo workspace snippets here. Each snippet is defined under a snippet name and has a scope, prefix, body and
// description. Add comma separated ids of the languages where the snippet is applicable in the scope field. If scope
// is left empty or omitted, the snippet gets applied to all languages. The prefix is what is
// used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders.
// Placeholders with the same ids are connected.
// Example:
// "Print to console": {
// "scope": "javascript,typescript",
// "prefix": "log",
// "body": [
// "console.log('$1');",
// "$2"
// ],
// "description": "Log output to console"
// }
"md-heading": {
"prefix": "md-heading",
"body": [
"<!-- ********************* -->",
"# Some heading",
"<!-- ********************* -->",
"$0"
],
"description": "Insert a commented heading"
},
"md-python": {
"prefix": "md-python",
"body": [
"```python",
"code",
"```",
"$0"
],
"description": "Python segment"
},
"md-powershell": {
"prefix": "md-powershell",
"body": [
"```powershell",
"code",
"```",
"$0"
],
"description": "Powershell segment"
}
}
Few notes
Multiple snippet files
There can be multiple snippet files in .vscode sub directory.
So once you have one, or another friend of yours have one, you can just copy them here with the same ".code-snippets" extension.
How to do you cue vscode to place the snippet at the cursor
By default the key sequence "ctrl-space" invokes a "suggest" feature and shows you the prefixes in the snippet. You just pick one and it will replace the snippet at the cursor.
You can figure out what key combination invokes this "Trigger Suggest"
领英推荐
You can also control where your snippet suggestion should appear in the list of available suggestion on the press of "ctrl-space" or if there is intellisense the first few characters.
For this
You have the option in settings to
Understanding tabbing in the snippets
Consider this snippet definition
{
"Function Definition": {
"prefix": "func",
"body": [
"function ${1:FunctionName}(${2:parameters}) {",
" ${0:// body}",
"}"
],
"description": "Insert a function definition"
}
}
Here is its behavior
The following will show up first
function FunctionName(parameters) {
// body
}
The place holders are
1: FunctionName 2: parameters 3: //body
Each tab will take you to that place and allows you to replace that word or segment
Reference
You will find here lot of details and future possibilities.
Using selections and variables
Notes on the insertion point
Photo Credit
Bowl with Daoist Figures, White Jade 1271 - 1378, Cleveland Museum of Art
Discover what I do not know. Work Ceaselessly in its pursuit.Programming, Curiosity, and Kindness is enough! Inspire others, if not enable.
9 个月Thanks Siva Kumar