Click here to Skip to main content
Click here to Skip to main content

Tagged as

Adding a Snippet to Visual Studio

, 20 Nov 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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:
    <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:
 
            <Title>Comment Block</Title>
will appear in the appropriate snippet context menu.
            <Shortcut>ComBlk</Shortcut>
Type this, and press [TAB] - the snippet will action.
            <Description>Code snippet for Comment block</Description>
will appear in a ToolTip in the snippet context menu.
            <Author>Microsoft Corporation / Paul Griffin modified</Author>
Any guesses? Laugh | :laugh:
            <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.
            <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.
            <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)

Share

About the Author

OriginalGriff
CEO
Wales Wales
Born at an early age, he grew older. At the same time, his hair grew longer, and was tied up behind his head.
Has problems spelling the word "the".
Invented the portable cat-flap.
Currently, has not died yet. Or has he?
Follow on   Google+

Comments and Discussions

 
GeneralHave you seen this excellent Snippet Designer plugin http:/... PinmemberGeorge Swan20-Nov-11 21:12 
Have you seen this excellent Snippet Designer plugin http://snippetdesigner.codeplex.com/

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141220.1 | Last Updated 20 Nov 2011
Article Copyright 2011 by OriginalGriff
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid