It has only a few TypeScript-oriented features, all other features work with other languages as well.
Full web support!
Snippets in VSCode are always contextless, they will be suggested in comments, string, everywhere. You can only restrict them showing only in specific languages, however this extension lets you construct snippets just on another level.
I recommend to use this extension instead of builtin snippets. There is Better Snippets: Migrate Native Snippets to Extension
command for migrating your native snippets to better snippets.
The main setting for constructing your snippets is betterSnippets.customSnippets
: example usage.
react
)when.fileRegex
to App.tsx?
)when.lineRegex
to \s*const
)function App() {
)Just because snippets are now aware of context, you can configure them to always show on top and/or show them only when exact snippet prefix is typed.
iconType
, fileIcon
or folderIcon
)sortText
, you can boost suggestion to top or bottom)This all can give extreme control for your snippets!
This extension comes with handy top-line builtin snippets for JS and Markdown languages. They can be disabled.
This extension comes with some defaults for your snippets.
They are set and configurable via betterSnippets.customSnippetDefaults
setting.
Some of defaults that important to understand:
code
. Banned locations:For all languages, location after dot e.g. in something.mySnippet
.
This was done as in most languages, after the dot you access the property and most probably don't want snippets.
For JS/TS langs, also snippets withing comments / strings.
aka hotstrings from AutoHotkey
You can also define so called typing snippets. Some sequence of typed letters will be replaced with snippet, for example:
cb
-> ($1) => $2
Configuration:
"betterSnippets.typingSnippets": [
{
"sequence": "cb ",
"body":"($1) => $2"
}
],
Note that:
resolveImports
or when
are supported!\n
can't be part of sequenceBelow are demos. See config for these demos above in example usage
You can use betterSnippets.extendsGroups
setting to extract common snippet data or even by creating your own snippet pack extension!
Example usage of betterSnippets.extendsGroups
:
"betterSnippets.extendsGroups": {
"reactUse": {
"when": {
"locations": [
"lineStart"
],
"languages": [
"react"
],
"otherLines": [
{
"indent": -1,
"testRegex": "function|=>|React.FC"
}
]
}
}
},
"betterSnippets.customSnippets": [
{
"extends": "reactUse",
"name": "useInitEffect",
"body": [
"useEffect(() => {",
"\t$1",
"}, []);"
],
"resolveImports": {
"useEffect": {
"package": "react"
}
},
},
]
You can reuse any custom or typing snippet data in this setting. For reusing name or sequence use nameOrSequence
property.
With special variables syntax (notice double $
in extendsGroups):
"betterSnippets.extendsGroups": {
"reactImport": {
"resolveImports": {
"$$FUNC": {
"package": "$$PACKAGE"
}
},
"when":{
"npmDependencies": ["$$PACKAGE"]
}
}
},
"betterSnippets.customSnippets": [
{
"extends": "reactImport",
"name": "useInitEffect",
"body": [
"useEffect(() => {",
"\t$1",
"}, []);"
],
"$FUNC": "useEffect",
"$PACKAGE": "preact"
},
]
Disabled by default, can be enabled with betterSnippets.enableExperimentalSnippets
setting.
However will be removed (migrated from here) in future releases.
Postfixes:
if
: something === 'here'.if
-> if (something === 'here')
Special:
useParams
for react-router
: demoMore docs coming soon, for now, you can look at changelog to see more about features