D.R.
D.R.

Reputation: 21224

Surround code block with curly braces?

Is there a shortcut to surround a given (selected) code block with curly braces?

VS 2015 + R# 2016.1

Upvotes: 34

Views: 28318

Answers (8)

seanm
seanm

Reputation: 121

At least in Visual Studio 2019, this is a built-in optional feature, albeit it is:

  1. Disabled by default
  2. Buried deep in the Options

To enable this feature, select ToolsOptions to open the Options dialog. In the left-hand pane that shows the options groups, expand the Text Editor group, then expand the C/C++ sub-group, then select the Advanced item. Now scroll all the way to the bottom of the right-hand pane and you'll see the following options under the Text Editor section:

Option Value
Enable Surround with Braces False
Enable Surround with Parentheses False

Change both of those to "True" (assuming you want both behaviors) and you're good to go!

Upvotes: 9

Efraim Newman
Efraim Newman

Reputation: 1066

This feature was just added to Visual Studio. From version 17.9 you can enable "Auto Surround with Quotes or Brackets" from Text Editor settings. Then you can select a block and just type a curly brace to surround the entire block. See this blog post for details.

Upvotes: 3

Amin Sanei
Amin Sanei

Reputation: 69

I am using the Auto Surround extension in Visual Studio 2022. It's easy to use, simple, and requires no configuration or snippets.

here is how to set it up:

  1. Install it from this link
  2. Reopen Visual studio
  3. Select code block that you want to surround.
  4. Press '{' or '(' or '['.The extension will automatically surround the selected code with the pressed symbol.

Upvotes: 2

baobab159
baobab159

Reputation: 61

AlexandreG's solution works great. From my side I can tell you have to save the file with .snippet extension. And 2nd tip: if you want to save a snippets together with system snippets you must save them here: C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC#\Snippets\1033\Visual C# or copy location from snippets file dialog

Upvotes: 2

AlexandreG
AlexandreG

Reputation: 1683

You do not need Resharper for this. You can use the "surround with" with a custom snippet.

Then you can select your text with Ctrl + K, Ctrl + S and select the snippet {} in the custom snippet. In order to create such a snippet :

  1. Create a ".snippet" file containing the content below
  2. Go to the snippet manager (Tools > Code Snippets Manager)
  3. Click import and select the file you just created

Use the following snippet :

<?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>Alexandre</Author>
   <SnippetTypes>
    <SnippetType>Expansion</SnippetType>
    <SnippetType>SurroundsWith</SnippetType>
   </SnippetTypes>
  </Header>
  <Snippet>
   <Code Language="csharp">
    <![CDATA[{ 
    $selected$ $end$ 
    }]]>
  </Code>
  </Snippet>
 </CodeSnippet>
</CodeSnippets>

Upvotes: 44

Noctis
Noctis

Reputation: 11783

using visual studio 2017 with resharper 2017, it seems like the way to do this would be to hit CTRL+E and either: Up,Up,Enter , or start typing the shortcut name (swb will match Surrond With block and make you happy.

I'm not 100% sure if this is VS or RS though, since the CTRL+E is VS 2017.
On the plus side, you can easily search for shortcuts and find new gems once you've hit the CTRL+E combo.

Update
This is part of Resharper. I'm using 2017.1.1 ultimate. You can find more surround with templates on their page for live templates.

Upvotes: 1

ulrichb
ulrichb

Reputation: 20054

As an as an alternative to Patrick's answer (Ctrl+E, U, 7) you could also use the extended Alt+Enter -menu of ReSharper 9+.

Alt+Enter, UP, UP, Enter, or

Alt+Enter, "bl", Enter

Screen

Unfortunately, I don't know a way to bind this to a shorter hotkey.

But if this is really important to you, you could try AutoHotkey.

Upvotes: 15

Patrick Quirk
Patrick Quirk

Reputation: 23757

The quickest built-in shortcut in the Visual Studio scheme I can find is Ctrl+E+U, 7 (surround with..., then choose option 7 which is curly braces).

I don't see a shortcut for it in Visual Studio's keyboard options, so that may be the best you can get.

Upvotes: 13

Related Questions