← Other topics

Quick Guide to Custom VSCode Snippets

Video Notes

Snippets in VSCode provide a way to easily inject commonly used code “snippets” into your files. In this guide, I will quickly show you how to create your own custom snippets. For a more comprehensive guide to snippets check out the official docs: Snippets in Visual Studio Code.

Create a new snippet file

To start, open the Command Palette with one of the following keyboard shortcuts...

...and start typing snippets to find and choose the option to Configure User Snippets:

Next choose New Global Snippets file...

...and give it a name, e.g. my-snippets:

This will open a new file called my-snippets.code-snippets:

Global snippet files can contain snippets for different language/file types. You can also create snippet files scoped to a particular language or project. Learn more here: Snippet Scope

Snippet structure

Within your new snippet file, an example is provided to show you the structure of a snippet:

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

Here’s a breakdown of each part:

Another example

Applying what we learned above, let’s add a snippet for a basic JavaScript function:

"Function": {
    "scope": "javascript,typescript",
    "prefix": ["func", "f"],
    "body": [
        "function $1($2) {",
        "\t$3",
        "}",
    ]
}

Note that the characters \t are used to inject indentation into the snippet body.

Test it

To invoke snippets, simply start typing in the prefix and choose your snippet from the options that appear.

Conclusion

The goal of this guide was to quickly cover the basics of creating snippets. If you want to learn more, including the following topics, be sure to check out the official VSCode docs on Snippets.

Unlock all the notes for $4

No subscriptions, no auto-renewals.

Just a simple one-time payment that helps support my free, to-the-point videos without sponsered ads.

Unlocking gets you access to the notes for this video plus all 200+ guides on this site.

Your support is appreciated. Thank you!

Payment Info

/
$4 6 months
$25 forever
Please check the form for errors
Questions? help@codewithsusan.com
← Other topics