28

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 for Haskell and I would like the same for F#. Is there a shortcut for this?

Marko Grdinić
  • 3,798
  • 3
  • 18
  • 21

3 Answers3

26

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 (

Visual Studio 2017 Text Editor Options

shybovycha
  • 11,556
  • 6
  • 52
  • 82
Lorien Brune
  • 830
  • 10
  • 16
8

Stumbled upon this when searching parenthesis surrounding for C# in VS2022. Another free method (non-hacky) to add in Dan Cundy's list: Auto Surround extension available for Visual studio 2022

NutCr4cker
  • 81
  • 1
  • 2
6

Paid Method

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

Resharper

Free Method

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>
Dan Cundy
  • 2,649
  • 2
  • 38
  • 65
  • 1
    Actually, I knew that Resharper has that based on [an article](http://enterprisecraftsmanship.com/2015/03/27/8-resharper-shortcuts-everyone-should-know/) that I read, but I thought that something like this might be possible without any expensive plugins. I am not doing dev in C# or any of the other VS languages apart from F#. – Marko Grdinić May 14 '16 at 11:35
  • Did you see the post with the snippet? Seems like there isn't much around for this. – Dan Cundy May 14 '16 at 11:36
  • 1
    I tried something similar to the above before I saw your edit, and I could get the snippet to import in F#. There might be something wrong with the [F# code snippet](https://visualstudiogallery.msdn.microsoft.com/d19080ad-d44c-46ae-b65c-55cede5f708b) plugin. Unlike C#, F# cannot use snippets without an extension. At any rate, I'll leave this for now. Since this is rather simple, it might be worth looking into how to write a `Shift+9` surround plugin specifically for this. – Marko Grdinić May 14 '16 at 12:31
  • Downvoted for advertising an expensive, subscription-based 3rd party extension for a functionality that could be accomplished via a short snippet in Visual Studio. – Robert Synoradzki Aug 24 '21 at 12:30
  • @RobertSynoradzki I also provided a free method after though using the short snipper you mention, perhaps I need to make that clearer. – Dan Cundy Aug 25 '21 at 12:37