Create a Snippet in Visual Studio 2010

Pre-2010 versions of  Visual Studio had a  snippets feature which allowed users to  save code blocks for later insertion. Visual Studio 2010 contains a numerous new snippets (especially ASP.NET snippets) and allows users to easily create their own snippets. Creating snippets in VS2008 was possible but without using third-party tools it was a tricky process.
VS 2010 greatly simplifies the process of creating custom snippets.
There are two types of snippets in VS 2010, Expansion Snippets which are inserted at the cursor and SurroundsWith Snippets which wraps around existing code.

Creating a Snippet in Visual Studio 2010

In this walkthrough we will create a code file header snippet.
  1. Add a new XML file to your project and name it sampleHeader.snippet (Visual Studio snippets always have the extension .snippet).
  2. Right-click on the editor window and select Insert Snippet to create a basic snippet template. Since we are creating an Expansion Snippet, remove the tag : <snippetType>SurroundsWith</snippetType>
  3. Alter the Title tag to read “Code File Header.”
  4. Change the Author tag to your own name.
  5. Change the Shortcut tag (which is the trigger word that activates your snippet) to “vstutor”
  6. Enter a description for this snippet, for example “Adds  header to the code file”.
  7. Since we are creating a  C# snippet alter the Language attribute of the Code tag so it reads <Code Language=”CSharp”> . Snippets can be creating for any language such as Visual Basic, XML etc
  8. The Code section contains the code block to be added. In this example, modify the code section till it reads:<Code Language=”CSharp”><![CDATA[***********************************

    Author: $Author$

    Date:

    Version:

    Purpose:

    ***********************************

    ]]>

    </Code>

The  finished version of the snippet appear as below:
<CodeSnippet Format=”1.0.0″ xmlns=”http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet”>

<Header>

<Title>Code File Header</Title>

<Author>Test Author</Author>

<Shortcut>vstutor</Shortcut>

<Description>Adds a header to a code file</Description>

<SnippetTypes>

<SnippetType>Expansion</SnippetType>

</SnippetTypes>

</Header>
<Snippet>

<Declarations>

<Literal>

<ID>Author</ID>

<Default>Alex Mackey</Default>

</Literal>

</Declarations>

<Code Language=”CSharp”>

<![CDATA[

***********************************

Author: $Author$

Date:

Version:

Purpose:

***********************************

]]>

</Code>

</Snippet>

</CodeSnippet>

Load Snippets into Visual Studio

Before the snippet can be used it will need to be loaded in Visual Studio. It is best practice to create a folder to hold all your snippets, to do this and load the folder follow the below steps:

1. Copy the sampleHeader.snippet file from the VS solution to your snippets folder.
2. In the main menu, navigate to Tools and then Code Snippets Manager to get to the Code Snippets Manager
3. Select Import
4. Select your snippet
5. Click OK
6. Visual Studio will then prompt you to confirm you wish to place the snippet in My Code Snippets folder.
That’s it,  the custom snippet is now ready for use, try it out by typing vstutor in a code file. In the event you don’t know or remember the snippet name you can  open the snippet dialog by right-clicking in the editor and selecting Insert Snippet ( or  press Ctrl + K and then Ctrl + X to fire up the Insert Snippet enhancement  to navigate through snippets using the keyboard).

Array

No comments yet... Be the first to leave a reply!