30

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

VS 2015 + R# 2016.1

D.R.
  • 20,268
  • 21
  • 102
  • 205

7 Answers7

42

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>
AlexandreG
  • 1,633
  • 2
  • 14
  • 18
  • 1
    Thanks! However, I think you forgot the following two closing tags at the end: – Wendelin Reich Sep 30 '18 at 13:32
  • @WendelinReich Indeed, forgot the spaces in the code indentation, now corrected! please vote for my answer if you like it, it encourages me to answer – AlexandreG Sep 30 '18 at 16:57
  • I wanted to push it further and add specific keybind for it. I simply couldnt. So only combination Ctrl+K Ctr+S and double Tab, works for me now. Is there way around it? – KrychuLittle Oct 07 '19 at 11:42
  • 3
    It is faster to cut the code add one `{` it will automatically create the closing bracket then paste it. +1 cause this solution works but I am using my old technique where I copy and paste it inside braces. – Tono Nam Apr 05 '20 at 18:01
  • 2
    The solution of **AlexanderG** is Great, but you must put snippets in `C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC#\Snippets\1033\Visual C#` as was wrote by **baobab159** below. If you put it in standard place, proposed by VS (`My Code Snippets`), in will unuseful, because you can't use the quick shortcuts - **CTRL + K + S + {} and after TAB**. (In other case, you will must at First, by mouse, select the catalog of the snippet `My Code Snippets` and after select the snippet self, It is terrible design and is brokening the Idea of use shortcuts) – Denis Sivtsov Sep 24 '21 at 17:00
15

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.

ulrichb
  • 19,610
  • 8
  • 73
  • 87
  • Thanks, guess I'll give the "ALT+ENTER bl ENTER" a try. Hopefully https://youtrack.jetbrains.com/issue/RSRP-101338 is reopened... – D.R. May 05 '16 at 23:51
  • 1
    done in 2017.3: https://youtrack.jetbrains.com/issue/RSRP-101338#comment=27-2529794 – CAD bloke Feb 07 '18 at 07:32
  • @CADbloke Thanks for the update, works amazing, what a time saver. – Sabuncu Feb 28 '19 at 13:55
  • FYI: You can do this out of the box in QtCreator by simply typing the opening curly brace (or parenthesis, quote, etc) while a block of text is selected. I miss this so much when having to work with VS. – Emile Cormier Sep 04 '20 at 21:14
  • 1
    @EmileCormier you can do it in any IDE or editor I've ever used besides VS. Even its younger sibling, VS Code, has it. Even Firefox's browser console has it! I don't understand how they could be missing this basic feature. – Hutch Moore Feb 05 '21 at 17:48
  • Thanks for Alt+Enter, plain VS has that option there now VS2022 – zORg Alex Jun 29 '22 at 09:48
  • It's honestly comical how hard they make this for no apparent reason. – Rocketman Dan Aug 22 '23 at 15:59
14

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.

Patrick Quirk
  • 23,334
  • 2
  • 57
  • 88
  • 4
    Visual Studio 2017 doesn't seem to have the curly braces option in the surround with menu. – Kyle Delaney Feb 07 '18 at 21:28
  • 3
    There is an option for this under C/C++ > Advanced options, under the Text Editor grouping at the bottom. But it doesn't seem to affect the C# editor. – J Bryan Price Aug 13 '18 at 00:46
  • This doesn't work in Visual Studio 2019 either. Pressing Ctrl+E starts a keyboard chord and doesn't open the Surround with... menu. – 2br-2b Jun 29 '23 at 17:17
8

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 Tools->Options 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!

seanm
  • 111
  • 1
  • 4
  • 12
    These options don't seem to be available for the C# text editor. – samp Feb 24 '21 at 21:54
  • Once you enable the above options, you can highlight text and press '{' to automatically surround the selected text with curly braces, or '(' for parentheses. – BurnsBA Sep 01 '22 at 01:04
2

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

baobab159
  • 51
  • 6
1

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.

Noctis
  • 11,507
  • 3
  • 43
  • 82
  • Tried VS17 and R# 2017.1 -> CTRL+E is R# on my side, and swb does not work at all :-( – D.R. Jul 31 '17 at 06:50
  • 1
    @D.R. Hmmm ... Tried with VS 2017 + code maid ... no love ... CTRL+K,CTRL+S will give you surround with options, but none for block. you can use any of the close enough ones (`using` for example) and simply change it, but yeah, not worth it if you can just type the brackets. – Noctis Jul 31 '17 at 12:01
  • This is ridiculous that VS can't do this. MonoDevelop does it automatically when you press the { key. – Kyle Delaney Feb 07 '18 at 22:02
  • 1
    @KyleDelaney sublime does as well. visual code does as well ..(both for curly and normal braces) .... :) – Noctis Feb 08 '18 at 09:53
0

I am using VS2022 V17.6.5, and in my opinion, the best and most convenient solution is to use Auto Surround extension.

  1. Install it from here
  2. Reopen Visual studio
  3. Selected code block which you wanna surround it.
  4. Press '{' or '(' or '[' and it it automatically surround selected code with pressed symbol.
Amin Sanei
  • 39
  • 6
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/34879804) – Chris Catignani Aug 27 '23 at 16:30
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 29 '23 at 08:08