Creating Own Code Snippet in Visual Studio

Published on Friday, February 7, 2014

You can create your own code snippet in Visual Studio for fast inputting.

For example, I often need input comment text which indicates the place which I added or modified in code when working with team members.

Microsoft provides tutorial here to create a code snippet.

Also, we can add multiple snippet sections in single snippet template file. see here.

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets
    xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>My Add Code Commend</Title>
      <Author>Myself</Author>
      <Description>My Commends</Description>
      <Shortcut>myaddc</Shortcut>
    </Header>
    <Snippet>
      <Code Language="CSharp">
        <![CDATA[
//Added By xxx Begin In $CommentTime$
//Added By xxx End In $CommentTime$
]]>
      </Code>

      <Declarations>
        <Literal>
          <ID>CommentTime</ID>
          <ToolTip>Replace with current time.</ToolTip>
          <Default>"Current Time"</Default>
        </Literal>
      </Declarations>
    </Snippet>
  </CodeSnippet>

<CodeSnippet Format="1.0.0">
    <Header>
      <Title>My Modify Code Commend</Title>
      <Author>Myself</Author>
      <Description>My Commends modify</Description>
      <Shortcut>mymodc</Shortcut>
    </Header>
    <Snippet>
      <Code Language="CSharp">
        <![CDATA[
//Modified By xxx Begin In $CommentTime$
//Modified By xxx End In $CommentTime$
]]>
      </Code>

      <Declarations>
        <Literal>
          <ID>CommentTime</ID>
          <ToolTip>Replace with current time.</ToolTip>
          <Default>"Current Time"</Default>
        </Literal>
      </Declarations>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>