Visual Studio - Is there a shortcut to insert parentheses around a selection?

前端 未结 3 1319
陌清茗
陌清茗 2021-02-19 03:55

I am playing with Atom and I really like how I can select an expression and press Shift+9 to insert ( and ) outside it. This is convenient

相关标签:
3条回答
  • 2021-02-19 04:03

    To add curly brackets:

    Just highlight your selection and press {

    Same applies for square brackets[] and brackes ()

    0 讨论(0)
  • 2021-02-19 04:14

    This is an in-built option in Visual Studio 2017. Go to Tools -> Options -> C / C++ -> Advanced, then navigate within the options dialog as shown on the screenshot below.

    Set the Enable Surround with Parentheses option to True.

    This works for C++, but the process ought to be similar for other languages.

    Once you click OK, you should be able to automatically insert parentheses around any selected text by typing only the first (

    0 讨论(0)
  • 2021-02-19 04:23

    You should check out a third party add-in like Resharper. They bundle a such abilities.

    Resharper

    There is another method noted by @Igor Zevaka.

    Here: Any way to surround code block with Curly Braces {} in VS2008?

    This allows you to create a snippet, and use a shortcut to use it.

    Here is a quick and dirty snippet to do just that.

    To Install:

    Save the code as SurroundWithBraces.snippet into "\Visual Studio Version\Code Snippets\Visual C#\My Code Snippets"

    To use:

    Select block of text. Press Ctrl+K, Ctrl+S Chose My Code Snippets, braces

    <?xml version="1.0" encoding="utf-8" ?>
    <CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
      <CodeSnippet Format="1.0.0">
        <Header>
          <Title>braces</Title>
          <Shortcut>braces</Shortcut>
          <Description>Code snippet to surround a block of code with braces</Description>
          <Author>Igor Zevaka</Author>
          <SnippetTypes>
            <SnippetType>Expansion</SnippetType>
            <SnippetType>SurroundsWith</SnippetType>
          </SnippetTypes>
        </Header>
        <Snippet>
          <Code Language="csharp">
            <![CDATA[{
            $selected$ $end$
         }]]>
          </Code>
        </Snippet>
      </CodeSnippet>
    </CodeSnippets>
    
    0 讨论(0)
提交回复
热议问题