Creating and using snippets in Visual Studio 2008

Visual Studio contains a number of predefined snippets. Snippets are usually available for common code that is used frequently allowing the developer to insert this code quickly without having to retype it every time. I often use the try and if snippets, but you can also create your own custom snippets. Recently I found myself to be endlessly creating a try/catch block where in the catch I was logging an exception using a custom logger class, so I decided to create a custom snippet to do this for me. In this post I’ll explain how to create a basic snippet, how to configure Visual Studio to use the snippet, and how to use it in code.

Snippets are picked up by intellisense. If in your code you type try you will see that intellisense will find the snippet.

In C# hitting tab twice will insert a try/catch block.  You will notice it places the cursor at the exception and allows you to type in what type of exception you want to catch or simple pressing enter will use Exception.

Certain snippets can be used to surround other code, the try snippet will do this.  If you select some code you can right click and choose the Surround with option from the context menu (alternatively you can use CTRL + B, CTRL + S), then choose the snippet you want to use to surround the code.

Snippet files are in XML format with a snippet extension.  Here is my snippet that I’ve called trylog.snippet.

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="<a href="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet</a>">
 <CodeSnippet Format="1.0.0">
  <Header>
   <Title>TryLog</Title>
   <Description>Inserts a Try Catch block using the Logger</Description>
   <Shortcut>trylog</Shortcut>
   <SnippetTypes>
    <SnippetType>SurroundsWith</SnippetType>
   </SnippetTypes>
  </Header>
  <Snippet>
   <Code Language="CSHARP">
    <![CDATA[try
   {
    $selected$$end$
   }
   catch(Exception ex)
   {
    Logger.LogError(ex);
    throw;
   }]]>
   </Code>
  </Snippet>
 </CodeSnippet>
</CodeSnippets>

Inside the Header element you can add the Title and Description etc.  The Shortcut is the string that intellisense will pick up when inserting your snippet.  In my example I have set the SnippetType to SurroundsWith which allows the snippet to be used with the Surround with feature of Visual Studio.

Inside the snippet element is where you add your Code element specifying the Language attribute.  You can then add your code which should be in CDATA tags. You can see I have created a simple try/catch block where in the catch I am using my custom logging component to log the error.

Inside your snippets there are certain literals that you can use to control the snippet’s behaviour.  In mine I am using $selected$ which is where the selected text will be placed when using the Surround with function.  I am also using $end$ which is where the cursor is placed after the snippet has been created.

You can also set up custom literals using the Declarations > Literal elements inside the Snippet element.  If we look at the predefined if snippet we can see how an expression literal has been created with a default of true.  When using the snippet the expression appears with the default and allows us to change it.

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="<a href="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet</a>">
 <CodeSnippet Format="1.0.0">
  <Header>
   <Title>if</Title>
   <Shortcut>if</Shortcut>
   <Description>Code snippet for if statement</Description>
   <Author>Microsoft Corporation</Author>
   <SnippetTypes>
    <SnippetType>Expansion</SnippetType>
    <SnippetType>SurroundsWith</SnippetType>
   </SnippetTypes>
  </Header>
  <Snippet>
   <Declarations>
    <Literal>
     <ID>expression</ID>
     <ToolTip>Expression to evaluate</ToolTip>
     <Default>true</Default>
    </Literal>
   </Declarations>
   <Code Language="csharp"><![CDATA[if ($expression$)
 {
  $selected$ $end$
 }]]>
   </Code>
  </Snippet>
 </CodeSnippet>
</CodeSnippets>

Once you have created a custom snippet it needs to be added in Visual Studio using the Code Snippets Manager.  Some tutorials state to select this from the Tools menu, although I’ve found by default it doesn’t appear in the tools menu.  To add it to the Tools menu click on Tools > Customize and choose the Commands tab in the new dialog.  In the Categories list scroll down to Tools; you should then see Code Snippets Manager in the list of Commands.  Click the Tools menu again then drag the Code Snippets Manager command onto the tools menu.  Alternatively you can access the Code Snippets Manager by using the keyboard shortcut CTRL + K, CTRL + B.

In the Code Snippets Manager if you are using C# change the dropdown at the top to Visual C#. You can then press the Add button to add a new folder for Visual Studio to look in for snippets.  I have a folder called snippets that contains all my custom snippets.

Click OK and Visual Studio is ready to use your snippet.  In a code window I can now start to type trylog  and intellisense will find it.  Hitting tab twice will then insert the code for me:


try
{

}
catch (Exception ex)
{
    Logger.LogError(ex);
    throw;
}

The cursor will be placed within the try section due to the use of the $end$ literal.  Alternatively I can select some text and Right Click > Surround with which will add my snippet with the selected text in the try block.

There are also a few useful functions that can be used within snippets that are documented here.

Posted on by Joe in Visual Studio 2008

Add a Comment