views:

386

answers:

2

Can I define custom "surround with" templates in Visual Studio 2008?

+9  A: 

Here you go, this is an example to set everything selected inside {}

In tools, codesnipet manager.

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"&gt;
    <CodeSnippet Format="1.0.0">
     <Header>
      <Title>{}</Title>
      <Shortcut>{}</Shortcut>
      <Description>Code snippet for {}</Description>
      <Author>Sérgio</Author>
      <SnippetTypes>
       <SnippetType>Expansion</SnippetType>
       <SnippetType>SurroundsWith</SnippetType>
      </SnippetTypes>
     </Header>
     <Snippet>
      <Code Language="csharp"><![CDATA[{ 
     $selected$ $end$ 
    }]]>
      </Code>
     </Snippet>
    </CodeSnippet>
</CodeSnippets>
Sergio
Thanks, do you know if there are more predefined literals (like $selected$, $end$)?
Piotr Owsiak
Just check the existing examples ;) That's how I learned.
Sergio
+1  A: 

Hi, I have an example for you. This snippet will surround the selected text with

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippet Format="1.0.0" xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"&gt;
  <Header>
    <Title>Surround in CDATA</Title>
    <Author>Sten Hougaard, 2010 - http://www.netsi.dk/wordpress&lt;/Author&gt;
    <Shortcut>shortcut</Shortcut>
    <Description>Surrounds selected data in CDATA</Description>
    <SnippetTypes>
      <SnippetType>SurroundsWith</SnippetType>
      <SnippetType>Expansion</SnippetType>
    </SnippetTypes>
  </Header>
  <Snippet>
    <Declarations>
      <Literal>
        <ID>name</ID>
        <Default>value</Default>
      </Literal>
    </Declarations>
    <Code Language="XML">
      <![CDATA[<![CDATA[$selected$]]><![CDATA[]]]><![CDATA[]]]><![CDATA[>]]>
    </Code>
  </Snippet>
</CodeSnippet>

The steps to make this example are this:

  1. Copy and paste these into a new XML file in Visual Studio
  2. Save it anywhere as for instance: "SnippetXML_SurroundWithCDATA.snippet"
  3. Open "Tools > Code Snippet Manager"
  4. Click "Import..." and locate the file you just saved, choose it and click "open"
  5. You now have the option to choose where it should be possibel to "run" the snippet. Choose one or more "scenaries"

The snippet is now ready for use. Try it using a relevant scenarie, for instance a XML file. Select some data and click Ctrl+K and Ctrl+S (or go through the menu). Locate the snippet and voila - your data has been surrounded with CDATA.

Netsi1964