If you are using Visual Studio 2005, you sure have encountered a new nifty feature called Code Snippets.
There are a lot of articles and blog posts surrounding this topic, so why another post? Well I’ll try to give give you a (hopefully) complete roundup on
- what Code Snippets are
- how you can use them
- how to create your own
- where to find some already done by others
- and how to install and manage them
I’ll describe the handling of Code Snippets in C#. In VB.NET Code Snippets behave a bit different, but since I am only using C#, I can’t tell you much about that.
Introduction to Code Snippets
Code Snippets are short pieces of reusable code for everyday use, which can be used in a very handy way.
Well, since Visual Studio 2003 you can write own pieces of code, which you can drag to the Toolbox for reuse, but using Code Snippets is A LOT sweeter.
You don’t have to use the mouse to drag them to your workplace anymore. Just type in the defined shortcut and press the TAB-key two times to insert a snippet. For browsing available Code Snippets you just press Ctrl+K,X or select Edit – IntelliSense – Insert Snippet… in the Visual Studio menu.
Create your own Code Snippets
Code Snippets are simply plain XML files, so you could write them in notepad with a little help of the Code Snippets Schema Reference. I would recommend taking a look at Snippy. Snippy is a free editor for creating and editing Code Snippets. Since the tools hosted on gotdotnet aren’t usually well documented, you might want to take a look at the overview at Visual Studio Hacks.
Ready made Code Snippets by other develolpers
There are some good locations on the web, where you can find ready to use snippets made by other developers.
After that you should take a look at GotCodeSnippets.NET, where many user defined Code Snippets are hosted.
In VB.NET you find a big bunch of already installed Code Snippets for any circumstance. Microsoft just published the same snippets for C# as a free download. Stupidly the installation isn’t really that, what you want. In the C# Snippet Parity post by Jeff Atwood, you find a better way of installing, which brings me to the last section of this article.
Managing Code Snippets
Since Jeff Atwood wrote nearly everything I planned to write in his C# Snippet Parity post ;), I try to make this short.
Jeff wrote about a way to install the additional C# Code Snippets by Microsoft using a system wide approach and a registry file. I prefer a per user installation by the following reasons
- Normally I am the only one on my PC coding
- I don’t like to install “personal” things in the Program Files folder, because they are gone after a clean system installation. (I use a different partition to store my files on, so I never have to worry when reinstalling my system.)
- The snippet paths are stored in the HKCU path in the registry anyway
- I don’t have to add new snippet paths to the registry
So, what to do? When installing Visual Studio 2005, the path Visual Studio 2005Code SnippetsVisual C#My Code Snippets will automatically be created inside the My Documents folder.
You can simply download the repackaged C# Code Snippets by Jeff and unpack them into the My Code Snippets folder. When calling the Visual Studio 2005 Code Snippets Manager using Ctrl+K,B or Tools – Code Snippets Manager…, you should find the new snippets inside the My Code Snippets folder.
You can simply reorganize your Code Snippets using the Windows Explorer. Now, how easy is that?
UPDATE: Jeff just wrote a nice macro for enumerating all installed Code Snippets.
Just a few installation hints, since some guys had problems with it:
- Download it from here
- Unpack and rename it to KeyboardShortcuts.vb
- Start the Macros IDE (Alt+F11 out of Visual Studio)
- Import it by right-clicking on MyMacros -> Add – Add Existing Item
The rest should be clear. If not, just take a look at Jeff’s post.
Links in this post (in order of appearance)
- Jeff Atwood – Code Snippets in VS.NET 2005
- MSDN – Creating and Using IntelliSense Code Snippets
- MSDN – Code Snippets Schema Reference
- Visual Studio Hacks – Snippy
- Roland Weigelt – Code Snippets
- MSDN – Visual Studio 2005 Code Snippets
- Jeff Atwood – C# Snippet Parity
- Jeff Atwood – Download C# snippets and registry file
- Jett Atwood – Snippet Enumeration Macro
This article is also filed here.