Following is an overview of getting started with Snippets. This will be the overview from 30,000 feet. If you want to get into the nitty gritty how-to then follow my links. As with all of my posts these days, this one will reference VS11 which is still in beta. Most of the concepts are the same for VS2010.
I would like to convince you that climbing the snippet learning curve is worth it because the view from the top is spectacular. In order to be convinced, you’re going to have to believe that you’ll save time. Here’s a brief comparison to help you believe…
|Operation||Keystrokes (Typing)||Keystrokes (Template)|
|Create a new hyperlink (HTML)||15||6|
|Create a simple one cell table (HTML)||40||5|
|Empty using statement (C#)||13||2|
|Try catch block||30||4|
I’m not exactly sure why Snippets in Visual Studio are so daunting for so many, but they are. I personally put off using them for a long time because creating and using them wasn’t just a “bam bam done” deal.
First, there’s the fact that the snippets are XML files, so you have to figure out the schema and what elements are expected. Then there’s the idea that Snippets are particular to languages. Then they need to be saved into a certain folder (C:\Users\<username>\Documents\Visual Studio 11\Code Snippets\Visual C#\My Code Snippets).
But once I took a few minutes to dig in and see how they work, I realized that they’re not difficult, and although the learning curve is steep, it’s also short and I think the power and configurability of Snippets necessitates it.
Let’s start off with some facts:
- Snippets are a way of saving typing and thus time (they also encourage more consistent code)
- Visual Studio offers Snippets
- ReSharper offers Live Templates which use a completely different technology to solve the same exact problem
- If ReSharper is installed and configured for IntelliSense then you can use both VS Snippets and R# Live Templates… this is my recommended configuration *
- Snippet Designer is a free plug-in for Visual Studio that helps you write Snippets
- Snippet Designer does not yet work in VS11 Beta
* in HTML/XML files, VS automatically creates closing tags for opening tags that you type. You can turn this off by going to Tools | Options | Text Editor | HTML | Formatting | Auto insert close tag.
Visual Studio – Snippets
Snippet functionality in VS is robust for certain. If it lacks anything, it’s ease of use and even that’s subjective. The process is to create a code snippet file – that is an XML file with a .snippet extension, drop it in the My Code Snippets folder, and then go to town with it. I obviously skipped over some details though, so just look at the Walkthrough – Creating a Code Snippet link below for more.
I would say that if you want to or have to (because maybe your employer is cheap and doesn’t want to spring for R# licenses) stick with native snippets, you should at least go get the Snippet Designer. It’s free, so if your employer doesn’t have a group policy against installing free tools, then go get it from the link below, and if they do then consider a new employer or send them my way and I’ll give them some financial justification with my 100 level economics that they won’t be able to argue against.
Snippet Designer is a free plug-in for Visual Studio that makes it easier to create and modify your code snippets. One of the best features is the ability to just highlight a block of code you’ve already written by conventional means, right click it, and choose to turn it into a snippet.
You can create or edit a snippet in Snippet Designer by either opening a .snippet file or by choosing New File in Visual Studio. It uses a custom editor in VS so you can stay in the same environment. Behind the scenes, Snippet Designer is just editing the snippet XML.
The bummer is that Snippet Designer doesn’t work in VS11 yet. Sheesh, you’d think we’re still in beta or something!
ReSharper – Live Templates
R# extends and enhances the Snippets in VS. If you allow R# to handle your IntelliSense (recommended), then you get the best of bot world – VS snippets and R# templates.
The two biggest advantages to R# templates over VS snippets are:
- the “macros” you can attach to place holders in templates and
- the ease with which you can save your templates in the solution (for you only), in the solution (for everyone that uses the solution), or on your computer (to be used across multiple solutions)
For in-depth help with templates see ReSharper Templates in ReSharper’s online documentation.