Posted in Editor, Powershell, Snippets, vscode

Easy Snippet – Small Tweaks

[These are pretty much 101 level posts]

Still distracted obviously, while on a conference call I was grazing through my VSCode PowerShell snippet file. I came across probably the most basic snippet and noticed it was not only lacking but missing some snippet magic:

        “Function”: {
            “prefix”: “func”,
            “body”: [
                “function $1() {“,
                “\t$0”,
                “}”
            ],
            “description”: “Function”
        },

Look at that mess. What is wrong?:

  1. When used it pastes the function shell but missing the closing curly bracket
  2. The $1() does nothing useful and we know the name of the function should be there.
  3. The $0 was right but again with no closing curly bracket why

So ashamed of myself, okay not really but a chance to improve with what I have learned

The revised version:

“Function”: {
            “prefix”: “func”,
            “body”: [
                “function $FunctionName {“,
                “\t$0# Code goes here\r”,
                “\\}”
            ],
            “description”: “Function”
        },
So what I have now performs the following when I call the Snippet now:
  1. Creates a basic function shell.
  2. The FunctionName term is highlighted first, so you enter the function name before beginning which just seems right.
  3. Once we give the function a name (Verb-Noun of course) we press Tab and we can start replacing “# Code goes here” with the contents of the function.
  4. The closing bracket is there and inline.

This is what it looks like in use.

Code_2018-04-26_16-29-48

 

 

 

Now to go back and fix up my other half-baked snippets and watch to see if I can discover new tricks as the community posts their snippets.

Author:

USMC, CVID, Technology, PowerShell... just general ramblings

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s