Creating your own snippets
Visual Studio snippets are a great way to quickly write repetitive chunks of code using the same basic structure and can save you a lot of time and typing. Snippets have been extended to work on more than just standard code files, and should be considered whenever you find yourself writing similar code over and over. Using snippets can save time and reduce the possibility of bugs: write the code correctly once and then re-use.
For example, you may want to generate a class signature that inherits from a specific base class you use in your application, or you may have a certain attribute that needs to be placed above method calls to enable logging, or you may have specific IDs you want to use in HTML elements to ensure CSS styles can be consistently applied to your web pages. Unfortunately, out of the box Visual Studio still doesn't have a built-in way of authoring snippets. Therefore, you will need to write some XML. Fortunately, it only takes a few minutes to create a snippet, and the time you can save once it exists makes it worth doing.
This recipe will show you how to create your own snippets and make them available inside Visual Studio.
Getting ready
Simply start Visual Studio 2013 and you're ready to go.
How to do it...
Create your own snippet using the following steps:
From the menu, choose File | New | File..., select XML File, and click on Open.
Populate the file using the following XML code:
<?xml version="1.0" encoding="utf-8"?> <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"> <CodeSnippet Format="1.0.0."> <Header> <Title>Wrap text in a span</Title> <Shortcut>spanned</Shortcut> <SnippetTypes> <SnippetType>Expansion</SnippetType> <SnippetType>SurroundsWith</SnippetType> </SnippetTypes> </Header> <Snippet> <Declarations> <Literal> <ID>id</ID> <Default>elementId</Default> </Literal> </Declarations> <Code Language="HTML"> <![CDATA[<span id="$id$">$selected$</span> is now in a span!]]> </Code> </Snippet> </CodeSnippet> </CodeSnippets>
Save the file as
spanned text.snippet
in yourDocuments
folder.From the menu, select Tools | Code Snippets Manager.... The shortcut is Ctrl + K, Ctrl + B.
Tip
If you are using Virtual Studio Express for Windows and cannot locate the Code Snippets Manager... on your menu, enable Expert Settings via Tools | Settings | Expert Settings.
Click on the Import... button. Select the file you saved in step 3 and click on OK.
Leave the location as My HTML Snippets, as suggested, and click on Finish. The snippet file will automatically be copied to the appropriate location in
Documents\Visual Studio 2013\Code Snippets
.In the Code Snippets Manager, change the Language to HTML and expand the My HTML Snippets location, as seen in the following screenshot, to confirm your snippet has been loaded. After confirming this, click on OK to close the dialog.
From the menu, select File | New | File..., then select General | HTML Page, and click on Open.
In the content of the
body
tag, enter<p>this is some text</p>
.Select the words
is some
, right-click on the selection, choose Surround With..., then My HTML Snippets | Wrap text in a span, and hit Enter.The snippet will be expanded and the contents of the
id
attribute for the span will be selected. Enter the textmyId
to replace the highlightedelementId
placeholder and hit Enter. The cursor will move to the end of the closingspan
tag.
How it works...
The Header
section is where the display title of the snippet is given, a shortcut name (in this case spanned
), a human-friendly description, and what type of snippet is being defined. Our example is in the Expansion
and SurroundsWith
categories. This means our snippet can be used with the selected text (SurroundsWith
) or at the cursor's current position in the editor (Expansion
).
The Snippet
section is where the bulk of the work is done. Visual Studio automatically scanned the code body of the snippet for an identifier placeholder of $id$
so that it could populate it with the default value and prompt you for your own value.
By declaring the snippet as a SurroundsWith
snippet, the selected text is passed to the $selected$
placeholder in the body. Since Expansion
is also supported, if you enter the snippet on a blank line, Visual Studio will still just generate the following text:
<span id="elementId"></span> is now in a span!
There's more…
There is a Snippet Designer project on CodePlex (http://snippetdesigner.codeplex.com/) that offers a GUI tool to make creating snippets much easier. It also enables you to select a section of code and export that as a snippet so that you have an easy starting point for making your own custom snippets.
Remember that snippets are more than just a simple text entry/replacement mechanism, and it's worth spending a little time looking through the full schema reference for snippets on MSDN at http://msdn.microsoft.com/en-us/library/ms171418(v=vs.120).aspx to get a better idea of what they can do for you.