Snippets in VS Code

I just discovered how to create snippets for VS Code. All this time I’ve been living without them. Why didn’t I look into this before? I don’t know.

To create / edit snippets, in VS Code, press Ctrl+Shift+p then type “snip”

To edit your snippets, select Preferences: Configure User Snippets, then select the snippets file or create snippets

In WSL Ubuntu, my c# snippet config file is located here: /home/jim/csharp.json

Here’s an example of a couple that I created:

{

    // Place your snippets for csharp here. Each snippet is defined under a snippet name and has a prefix, body and 

    // description. 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”: {

    //  “prefix”: “log”,

    //  “body”: [

    //      “console.log(‘$1’);”,

    //      “$2”

    //  ],

    //  “description”: “Log output to console”

    // }

    “Property”: {

        “prefix”“prop”,

        “body”: [

            “public ${1:type} ${2:MyProperty} {get; set;}”

        ],

        “description”“Create a property with ‘private type varName {get;set;}'”

    }

    “PropertyFull”: {

        “prefix”“propfull”,

        “body”: [

            “private ${1:type} ${2:myVar};”,

            “”,

            “public $1 ${3:MyProperty}”,

            “{“,

            ”   get { return $2; }”,

            ”   set { $2 = value; }”,

            “}”

        ],

        “description”“Full property with private backing”

    }

}

After you save the file, in the VS Code editor for your C# project, simply type the snippet prefix, such as “prop” or “propfull” then press tab and fill in the values

 

Snippets are such handy things…..

 

There are a bunch of topic-specific config files available for use, too. For more info, see: https://code.visualstudio.com/docs/editor/userdefinedsnippets

 

Enjoy!

 

And don’t forget to wash your hands.

 

.