Tag Archives: Markdown

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.

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.