Code Snippets – the whole enchilada

Code Snippets – the whole enchilada
February 26, 2006 Jens

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.

Also, you can define placeholders inside a snippet, which are replaced during insertion. You can take a look at a nice demo and how to by Jeff Atwood.

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.

First I’d like to point you to the snippets done by my colleague Roland. He did some for inserting Properties with Prefix Notation, Events, NUnit Test Methods, etc.

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.

CodeSnippetsManager_3

InsertCodeSnippetDialog_1f29aa60-ffa1-400e-a90b-912d9c399522

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)

This article is also filed here.

Hello! I’m Jens. A User Experience Specialist and Developer based in Bonn, Germany.

2 Comments

  1. Maruis Marais 18 years ago

    Good post explaining how code snippets work, thanx for explaining a few new concepts.

    I’ve created a VS2005 Unit Testing code snippet library. This library contains both MS and NUnit code snippets and covers attributes, assert methods and code fragments (class, methods, full implementation, etc.). If you are interested, have a look at http://exceptionz.blogspot.com/2006/02/visual-studio-2005-unit-testing-code.html or the full article can be found on The Code Project at http://www.codeproject.com/dotnet/UnitTestCodeSnips.asp.

    Once you started to use code snippets you will be amazed at how much time the snippets save you.

    • Author
      Jens Schaller 18 years ago

      Thanks! I’ll definitely take a look at your UnitTesting snippets.

Leave a reply

Your email address will not be published. Required fields are marked *

*