Creating your own snippet in VS Code within 1 minute

Creating your own snippet in VS Code within 1 minute

You write the same basic boiler plate code every-time you create a new file and wish if I could use some snippet to write those. So in this short article, I'll tell you how you can create your own snippet in VS Code within 1 minute, which will save your lot of time and effort. For the purpose of demo I'll show how you can create code snippet for C++.

Step 1: Open your VS Code and click on the settings button in the bottom-left corner.

Arrow pointing towards setting icon in vs code

Step 2: Now click on User Snippets.

Click on User Snippets option

Step 3: You’ll now see a dropdown at the top for a list of various files in JSON format. Now search for C++ or you can create of your own,

Screenshot (52).png

Step 4: Delete everything for the file and write the following:

"cpp snippet": {
        "prefix": "cpp",
        "body": [
            "#include<iostream>",
            "using namespace std;",
            "int main(){",
            "    $0", //$0 for where you want your cursor to be
            "    return 0;",
            "}"
        ],
        "description": "to produce the short snippet for cpp"
    }

Your final code should look like this:

final code snippet should look like this

Now save this file. Create a new C++ file and write cpp, you will have like this on your screen and press enter:

Example of using code snippet

Here,

  • prefix defines how you want to call your code snippet.
  • body defines what should be the content of code.
  • description gives a short bio about the snippet

Kudos, you have finally created your own code snippet.

If you have come this far, hope you like my article. You can connect with me on My LinkedIn Account or send me mail on dashashutosh1999@gmail.com.