Visual Studio wrap selection in quotes?
You can use the following command (C# language) with my Visual Commander extension to wrap a selected text block with quotes:
public class C : VisualCommanderExt.ICommand
{
public void Run(EnvDTE80.DTE2 DTE, Microsoft.VisualStudio.Shell.Package package)
{
EnvDTE.TextSelection ts = DTE.ActiveDocument.Selection as EnvDTE.TextSelection;
ts.Text = '"' + ts.Text + '"';
}
}
This might be overkill, but ReSharper offers a utility called Surround With that offers a templated mechanism for surrounding blocks of text. It doesn't look like they have a template out of the box for quotes, but you should be able to easily create one:
Plugin Description: https://www.jetbrains.com/resharper/help/Templates__Applying_Templates__Surrounding_Code_Fragments_with_Templates.html
The "Surround with" option is available in Visual Studio also without ReSharper. It doesn't contain the option to wrap in quotes. But it's possible to extend the snippets with custom wrappers. Also with double quotes. To do that:
- Click
File
and then clickNew
, and choose a file type ofXML
. - On the
File
menu, clickSave
. - In the
Save as
box, selectAll Files (*.*)
. - In the
File name
box, enter a file name with the.snippet
file name extension. - Click
Save
. - Add this code to the file.
Code
<CodeSnippet Format="1.1.0" xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<Header>
<Title>doubleQuotes</Title>
<Author>Microsoft Corporation</Author>
<Shortcut>"</Shortcut>
<Description>Wrap in double quotes</Description>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
<SnippetType>SurroundsWith</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>selected</ID>
<ToolTip>content</ToolTip>
<Default>content</Default>
</Literal>
</Declarations>
<Code Language="CSharp">"$selected$"</Code>
</Snippet>
</CodeSnippet>
Save the file.
- Open
Tools
->Code Snippets Manager
. - In Language section select "Visual C#".
- Click
Import
and browse to the snippet you just created. - Check
My Code Snippets
and clickFinish
and thenOK
.
To use it: Select text -> right click -> select "Surround with..." -> My Code Snippets -> doubleQoutes
Alternatively: Select text -> hit Ctrl + K, S -> My Code Snippets -> doubleQoutes
I got the idea for this solution from this answer where the author shows how to wrap code in custom html tags.