Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Visual-Studio

Adding a Snippet to Visual Studio

5.00/5 (3 votes)
20 Nov 2011CPOL2 min read 20.6K  
It is really easy to do once you get the idea how, but the Microsoft documentation could be a lot clearer - so here is a simple sample with full instructions.
This started as a C# forum question:
How can I automatically enclose a block of code with "/*" and "*/" comment markers?
I knew you could produce your own snippets, but I'd never tried. How hard can it be?

Quite hard really - Microsoft doesn't appear to like publishing the relevant information (which is pretty simple) in a easily found and read place. They publish bits here, and bits there, but nothing together and easy to follow.

Once you have worked your way through the pages, it is really a very easy thing to do.

This assumes you have VS2010 - I don't have 2008 installed any more, so I can't check it backwards, but the process will be very similar, with different paths, mostly.

  1. Create a new file in VS: On the menu "File"..."New"..."File...". In the dialog, select "XML File" and press "Open"
  2. Replace the file content with:
    HTML
    <CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
        <CodeSnippet Format="1.0.0">
            <Header>
                <Title>Comment Block</Title>
                <Shortcut>ComBlk</Shortcut>
                <Description>Code snippet for Comment block</Description>
                <Author>Microsoft Corporation / Paul Griffin modified</Author>
                <SnippetTypes>
                    <SnippetType>Expansion</SnippetType>
                    <SnippetType>SurroundsWith</SnippetType>
                </SnippetTypes>
            </Header>
            <Snippet>
                <Declarations>
                    <Literal>
                        <ID>Comment</ID>
                        <ToolTip>Comment Block</ToolTip>
                        <Default>MyComment</Default>
                    </Literal>
                </Declarations>
                <Code Language="csharp">
                    <![CDATA[/* $selected$ */$end$]]>
                </Code>
            </Snippet>
        </CodeSnippet>
    </CodeSnippets>

    You don't actually need all of that, but it shows all the bits you will probably need.
  3. Save your file, as "CommentBlock.snippet" in the folder:
    C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC#\Snippets\1033\Visual C#


  4. Done. (Obviously, you can use the "VB" folder instead of the "VC#" one for VB snippets - the structure is very similar).


A quick tour round the various parts of this:

HTML
<Title>Comment Block</Title>

will appear in the appropriate snippet context menu.
HTML
<Shortcut>ComBlk</Shortcut>

Type this, and press [TAB] - the snippet will action.
HTML
<Description>Code snippet for Comment block</Description>

will appear in a ToolTip in the snippet context menu.
HTML
<Author>Microsoft Corporation / Paul Griffin modified</Author>

Any guesses? :laugh:
HTML
<SnippetTypes>
    <SnippetType>Expansion</SnippetType>
    <SnippetType>SurroundsWith</SnippetType>
</SnippetTypes>
"Expansion" controls if it is in the normal "Insert snippet" context menu, "SurroundsWith" controls the Surround With menu.
HTML
<Declarations>
    <Literal>
        <ID>Comment</ID>
        <ToolTip>Comment Block</ToolTip>
        <Default>MyComment</Default>
    </Literal>
</Declarations>
is not needed in this snippet. These allow you to add special fields in the same way that the "prop" (automatic property) snippet does. To use them in your code, surround them with '$' signs.
HTML
<Code Language="csharp">
    <![CDATA[/* $selected$ */$end$]]>
</Code>
This does the work! "selected" and "end" are reserved words: the first is replaced with the selected text when you activated the snippet, the latter specifies the cursor position to finish with.

[edit]Code block tag encoded by mistake - fixed. - OriginalGriff[/edit]

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)