First of all, to create a new snippet you have to open the Command Palette, type “Configure User snippets” and press ENTER. After that VsCode suggests you some options, two of these are:
name of your project
‘…$HOME/Library/Application\ Support/Code/User/
$HOME/.config/Code/User/
you can find more about that configuration here
Instead, the project snippets are saved inside of the .vscode
folder in the project’s root and the snippet file has this form {snippet-name}.code-snippets
.
{
// Place your global 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"
// }
}
{
"Fetch Request": {
"scope": "javascript,typescript",
"prefix": "fetch",
"body": [
"fetch(\"$1\")",
" .then(response => response.json())",
" .then(data => $2)",
" .catch(error => console.error(error));"
],
"description": "Fetch request"
}
}
{
"React Functional Component with Props": {
"scope": "typescriptreact",
"prefix": "rfcpt",
"body": [
"import { FC } from 'react'",
"",
"type ${1:$TM_FILENAME_BASE}Props = {",
" $2",
"}",
"",
"const ${1:$TM_FILENAME_BASE}: FC<${1:$TM_FILENAME_BASE}Props> = (props) => {",
" return <>$3</>",
"}",
"",
"export default ${1:$TM_FILENAME_BASE};"
],
"description": "React.FC component with Props in TypeScript"
}
}
This feature is widely used in extensions, and you can find many extensions that create for you snippets, for instance: Angular Snippets, ES7+ React/Redux/React-Native snippets…