Can I define custom "surround with" templates in Visual Studio 2008?
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">
<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>
I have an example for you. This snippet will surround the selected text with <![CDATA[...]]>
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippet Format="1.0.0" xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<Header>
<Title>Surround in CDATA</Title>
<Author>Sten Hougaard, 2010 - http://www.netsi.dk/wordpress</Author>
<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:
- Copy and paste these into a new XML file in Visual Studio
- Save it anywhere as for instance: "SnippetXML_SurroundWithCDATA.snippet"
- Open "Tools > Code Snippet Manager"
- Click "Import..." and locate the file you just saved, choose it and click "open"
- You now have the option to choose where it should be possible to "run" the snippet. Choose one or more "scenarios"
The snippet is now ready for use. Try it using a relevant scenario, 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.
In the event anyone else visits this question searching for more examples, here's my snippet to surround the block of code with a stopwatch:
<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>Stopwatch</Title>
<Shortcut>SW</Shortcut>
<Description>Code snippet for Stopwatch</Description>
<Author>Bernhard Hofmann</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
<SnippetType>SurroundsWith</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Code Language="csharp"><![CDATA[var tacho = new System.Diagnostics.Stopwatch();
tacho.Start();
$selected$ $end$
tacho.Stop();
System.Diagnostics.Trace.WriteLine(string.Format("Elapsed: {0}", tacho.Elapsed));
]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
Also, if you save it as Stopwatch.snippet in My Documents\Visual Studio ????\Code Snippets\Visual C#\My Code Snippets, then it'll automagically appear in Visual Studio without needing to visit the Tools menu.