Code Snippets – a way to be more efficient in writing code

One of my favorite new features in Whidbey is Code Snippets.  Code snippets are xml files stored on your machine that allow you to insert chunks of predefined code and then move quickly between common places that need to be filled in with values relevant to the current method or class, and then continue editing your file as usual.  For example, in a C# file you can type the word “for” and then hit the tab key.  This will insert a code snippet.  You’ll notice that you have inserted code similar to the following:


for (int i = 0 ; i < length ; i++)





The first “i” is highlighted in yellow.  You can now change that value “i” to something more meaningful, say iCount.  Hit the tab key again and your caret will move to the next yellow field “length”.  As you did this, all the other “i” values updated to be “iCount”.  You can hit tab again to go back to iCount and then hit enter to complete your task.  Your caret will now be placed in the middle of the loop ready for you to enter content.


To see all the code snippets that you have on your machine you can either go to the Tools menu and select “Code Snippet Manager…”, or you can use the insertion UI to browse through them and insert one.  To get to the insertion UI, right click in a C# file, choose the “Intellisense” menu and then choose “Insert Expansion”.  Selecting one of the snippets from the intellisense list here will insert that snippet into your file like it did when you typed “for” and tab.  To modify your snippets and customize them, all you need to do is go to the Code Snippet Manager, find the snippet you want, and look at the location field for the path to the snippet file.  Simply open this file in the xml editor and modify it.  As soon as you save it, you can go ahead and use that snippet again and your modifications will be there.


Here is an example of a snippet file:


<?xml version="1.0" encoding="utf-8" ?>


<CodeSnippet Format="1.0.0">


        <Title>for-iteration by index</Title>


        <Description>Expansion snippet for 'for' loop</Description>
















            <ToolTip>Max Length</ToolTip>



        <Code Language="csharp" Format="CData"><![CDATA[for (int $index$ = 0; $index$ < $max$; $index$++)


                  $selected$ $end$







The most interesting things that you might want to change at first are the shortcut tag or the code block.  Changing or adding anything in the CDATA block will change what appears in your file when inserting it.  Changing the shortcut value changes what you type before hitting tab to insert the snippet.  The interesting portions of how to create your own snippet comes in with the literal tags.  Each literal corresponds to a value that you will tab between (the yellow highlighted portions in the editor).  You’ll notice that the first literal “index” is shown down in the code block with $ on each side of it.  The value that is inserted into the editor by default for this is “i” which is found in the Default tag for this literal.  The “max” literal is the next field in this for loop.  You’ll see two values in the code block that look like literals but aren’t defined anywhere.  $selected$ and $end$ are reserved literals.  $selected$ tells us that if you highlight a block of code and insert the for loop, we will insert the snippet around that selected text so that your selected text becomes the body of the loop.  The $end$ tag tells us where to put the caret when you hit the enter key after tabbing through the snippet fields.


If you are using VB, you can also insert these snippets by right clicking and choosing “Insert Snippet…”.  In Beta2 you should be able to use the shortcut like we did in C# by typing into the editor and hitting the tab key.  Also in Beta2 we will be adding xml snippets.


In my next post, I’ll talk a little more about other aspects of the snippet schema including how functions play into the literals.


There is a tool snippy that a member of the C# team (my husband Gus) wrote to create code snippets.  Once you create a snippet, save it to disc and then in the Code Snippet Manager you can tell us to look in that location for snippets or you can save the snippet with the other ones you already have on disc and we will automatically pick it up.


Let me know what you think of this feature…



Comments (14)

  1. damien morton says:

    Eesh – cut-and-paste inheritance, macros that arent macros. If you are using this, you are probably missing opportunities to reduce the ammount of code you are producing.

  2. I think this is a great feature….. An associate of mine and I developed a similar type of tool (Snippets and Snippet Editor) quite a few years ago (Visual Studio 5/6) timeframe. For me this is a welcomed addition.

  3. Eric Wilson says:

    Is the format for the XML schema documented yet? I tried searching for it on MSDN, but came up with nothing.

  4. Very good!! FYI – Borland created something like this in there .NET tools in Delphi 8. In addition, I think that a way to add stop points so that you can quickly step through the user defined parts of the newly created section of code would help out equally well. There is a tool for Delphi that has this functionality called <a href="">Castalia</a&gt;. Take a look at it. I use it when developing Delphi 7 Win32 and it is a very useful add-in.

  5. Alex says:

    I like the idea but I found the implementation a little clunky – perhaps if the context menu insert snippet command were to break out a list of snippets in a sub menu (like windows favourites) rather than pop up another different menu.

    I know this will mean adding more heirarchy but in the end that might be for the best if you use the feature a lot.

  6. One of the big new features of the Visual Studio 2005 release for the Editor team and for the language…

  7. When you insert a code snippet, the editor highlights the fields (variables, values, etc) you can modify

  8. When you insert a code snippet, the editor highlights the fields (variables, values, etc) you can modify

  9. In Visual Studio 2005, we introduced code snippets. The VS Editor Team Blog has a nice write-up on code

Skip to main content