Fun With VSCode Snippets for Markdown and PowerShell

Introduction

I have a confession. I love Markdown. There I said it, my confession is out in the open for all to see.

Seriously though, I do find this documentation language very useful, and easy to use. With just a few commands I can produce a nicely formatted document that can be displayed in my code editor, as well as on platforms like GitHub. I’ve even begun authoring these blog posts in Markdown.

A big reason for me is the ability to integrate it into my projects. VSCode, as well as the full blow Visual Studio, support Markdown (with of course the proper extensions installed). When I create a new PowerShell project in VSCode, I can store the projects documentation in Markdown format right alongside the PowerShell code.

Speaking of VSCode…

VSCode Snippets

A great thing about VSCode is the ability to create your own code snippets. A snippet is a text replacement system where I can enter a few characters of text and VSCode will then replace it with the full snippet. Snippets are activated using the Intellisense feature of VSCode.

One example, I have a standard header I put at the top of my PowerShell scripts. When I defined this header as a snippet, I named it psheader. Now I can enter pshead into my PowerShell script. VSCode’s intellisense feature will prompt me with the list of snippets I have that being with pshead. I can then pick the one I want (psheader), press enter and the snippet of psheader will be replaced with the full text of my file header.

By default, pretty much every language in VSCode has the ability to handle snippets.

Except Markdown.

Markdown Snippets in VSCode

Shocking right? How could such an awesome language like Markdown not have snippets? (And yes, Markdown may not be a language in the strictest sense, but it’s a close enough word for now.)

Well it’s possible to enable Markdown snippets in PowerShell. Sometime back I created a GitHub project that shows you how to enable and use snippets for Markdown. In addition, I included my snippets for both PowerShell and Markdown.

Rather than reiterating everything here, I’ll just point you to that repository.

https://github.com/arcanecode/VSCode_User_Snippets

The main file in the repository, ReadMe.md gives a brief overview and explanation on how to use snippets.

The file Enable_Intellisense_in_Markdown.md does just what is says, shows you how to enable intellisense for Markdown in VSCode.

In VSCode, you can use certain replacements in your snippets. For example, you can embed the $CURRENT_YEAR snippet variable in your snippet (no matter what language) and when the snippet is generated into your code, it will replace the $CURRENT_YEAR with the actual current year.

I included a file, Snippet_Reference.md that lists the various snippet variables and gives a bit more explanation on how to use them.

If you aren’t familiar with Markdown, or don’t use it very often, you’ll find the file Markdown_Cheatsheet.md useful. It has a list of the most often used Markdown formatting commands.

Finally I included two .json files. These are the snippets I use for PowerShell and Markdown on my system. You can use these as a guide in creating your own snippets, or copy the ones you find useful onto your VSCode installation.

If you use the VSCode sync settings feature, they you will be happy to know snippets are included as part of the sync process. You can modify your snippet files on one computer and they will be copied to all the other computers you sync VSCode on.

Conclusion

This was a brief post that primarily served to bring awareness to snippets, as well as the ability to use them with Markdown files. Go take a look at the repository and with just a little effort I believe you’ll find yourself becoming much more productive with the addition of snippets to your toolbox.

Exclude A File From Git Source Code Control in VSCode

Like many developers, I use VSCode for my projects. Primarily PowerShell, but also other things like the PHP code used for my ham radio club website or markdown projects.

It can be useful to have extra files, that I don’t need or want to have saved in Git. One example, I often have a “scratchpad.ps1” file. It’s a place I can prototype code, test out ideas, before I copy them into my main project. If this file gets lost, or damaged, I don’t really care as it’s just temporary.

Another example stems from my need to demonstrate code on video, for my Pluralsight courses, or live at SQL Saturdays and code camps. I often need to login to a website or database, and don’t want to have my credentials hard coded in my script for everyone to see.

To solve this, I simply place my needed information in a text file, then use Get-Content (with the -Raw switch) to read it into a variable. I don’t want this text file though to be placed in my public github repositories.

Excluding a file is simple. In the root folder of your source controlled project is a folder named .git. (Note the period on the front, also note that on some operating systems it may be hidden by default.) Under it is another folder called info.

In .git/info is a file called exclude (with no extension). To it, simply add the names of the files you want to exclude from source control. Here’s an example:

# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~
uid.txt

The lines that begin with a pound sign (#) are comments, and are included by git.

Under it is the file I added, uid.txt. This file is will now be excluded from any git commits, and will not be uploaded to github or whatever tool you use for git. You can see this in the VSCode file tree, the file will appear in an off color font. In a dark mode theme, this will be a gray.

The arrow points to the uid.txt file that was excluded from the project.

That’s all there is to it. Now you can include extra files, such as scratch pads, notes, or even passwords that you don’t need (or want) to have as part of your git repository. Just keep in mind it’s your responsibility to back these files up.

VSCode User Snippets for PowerShell and MarkDown

I’ve been working a lot on a new course for Pluralsight, Everyday PowerShell 7 for Developers. I’ve been cranking out a lot of code as a result, PowerShell, obviously, as well as Markdown for documentation.

I’m finding the use of VSCode’s User Snippets to be extremely useful for developing this course. Snippets have allowed me to store my most often used code as templates, quickly manifesting new sections of code.

In PowerShell I’m finding these especially useful for writing Pester tests. Most tests fall into a few simple patterns, using a snippet I can quickly create the bulk of my test, leaving me to fill in the few remaining pieces.

If you know Markdown, you know some of the syntax can be a bit arcane. Using snippets makes it easy to insert commonly used ones, such as being able to type in a snippet name of mdlink and get the correct syntax to appear for a hyperlink.

It also helps with consistency. A small example, for doing italics in Markdown, you can use a singe underscore or a single asterisk. For bold, two underscores or asterisks.

I decided (for no particular reason) to use underscores for italics and asterisks for bold. If it’s been a while since I wrote Markdown though, I may not always remember. So I setup mditalic and mdbold to insert the correct formatting for me.

I’ve placed my snippets in a repository on my github site:

https://github.com/arcanecode/VSCode_User_Snippets

If you aren’t familiar with how to use User Snippets, I have a quick overview in a Markdown file in the repository.

Speaking of Markdown, there is one issue with VSCode. By default, intellisense is not enabled in VSCode for Markdown. While snippets can be used without intellisense, it’s much easier to use them with intellisense turned on.

In the repository I have a Markdown  file with information on how to turn intellisense on in VSCode for Markdown files.

Be aware there are also user snippets available through the Extensions Marketplace in VSCode. Just go into the Extensions, and enter Snippet into the search bar.  I just didn’t happen to find any that quite suited my needs, hence I created my own.

You’re welcome to copy and use the user snippets, just some or all of them, as you need. Do be aware some of them have my name and websites, such as the author info snippet I use for the bottom of all my Markdown files, or the header snippet for the top of my PowerShell files.

I hope you find user snippets as helpful as I do.