Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio wrap selection in quotes?

Is there is a way to wrap a selected text block with quotes? In visual studio I have not found a extension or plugin I am just looking for a simple way to do it. Is there a way to add that functionality?

like image 749
Jesse Avatar asked Apr 20 '15 20:04

Jesse


People also ask

How do I wrap text around a quote in Visual Studio?

Select any text in Visual Studio and right click. Then select Surround Selection With and then select the option you want eg. Double Quotes to get the text wrapped around that.

How do you add quotation marks in Visual Studio?

To place quotation marks in a string in your code In Visual C# and Visual C++, insert the escape sequence \" as an embedded quotation mark.


3 Answers

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:

  1. Click File and then click New, and choose a file type of XML.
  2. On the File menu, click Save .
  3. In the Save as box, select All Files (*.*).
  4. In the File name box, enter a file name with the .snippet file name extension.
  5. Click Save.
  6. 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.

  1. Open Tools -> Code Snippets Manager.
  2. In Language section select "Visual C#".
  3. Click Import and browse to the snippet you just created.
  4. Check My Code Snippets and click Finish and then OK.

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.

like image 52
PiotrWolkowski Avatar answered Sep 16 '22 15:09

PiotrWolkowski


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:

enter image description here

Plugin Description: https://www.jetbrains.com/resharper/help/Templates__Applying_Templates__Surrounding_Code_Fragments_with_Templates.html

like image 43
Philip Pittle Avatar answered Sep 20 '22 15:09

Philip Pittle


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 + '"';
    }
}
like image 25
Sergey Vlasov Avatar answered Sep 19 '22 15:09

Sergey Vlasov