Any way to surround code block with Curly Braces {} in VS2008?
Asked Answered
S

9

46

I always find myself needing to enclose a block of code in curly braces { }, but unfortunately that isn't included in the C# surround code snippets, which seems to be an oversight. I couldn't find anything on building your own surround snippets either (just other kinds of snippets).

I am actually running Resharper too, but it doesn't seem to have this functionality either (or I haven't figured how to activate it).

We have a coding standard of including even a single line of code after an if or else in curly braces, so if I could just make Resharper do that refactoring automatically that would be even better!

Smukler answered 4/5, 2010 at 22:55 Comment(2)
if its just to refactor the one liners.. isnt a surround snippet more key presses than just typing in the { } ? Steps: Click beginning,{,END,} = so 3 keypress, 1 click. vs select text, ctrl k, s, first few letters of snippet name,enter= at least 1 click, minimum 4 key presses (but only if intellisense takes you to the snippet by typing 1 letter)Format
What a pain in the ***. I'd love to simply select the text I wish to surround then the character I wish to surround with, be it {, [, ( or ", just like I do on PyCharm. Even with ReSharper an obscene amount of key presses is needed for every shortcut. Is there by chance any update as to this functionality in the latest versions?Oilcup
P
52

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

To Install:

Save the code as SurroundWithBraces.snippet into "<my documents>\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>
Pierrepierrepont answered 4/5, 2010 at 23:45 Comment(2)
Works great! To avoid showing of the My Code Snippets folder when pressing the shortcut you can put the snippet just inside the internal storage of VS snippets. In my case for VS 2015 and C# the path is: C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC#\Snippets\1033\Visual C#Overseer
Can snippets have parameters? I mean I can also just copy this snippet 3 times for square brackets and parentheses but yes, just wondering :)Cohune
C
38

In ReSharper 4.5, curly braces are included as one of the built-in 'Surround Templates':

  1. Select the text that you want curly braces around.
  2. ReSharper -> Code -> Surround With... -> {}

    or

    ALT + R -> C -> S -> 7

    or

    Ctrl+E, U -> 7 (Visual Studio scheme)

    or

    Ctrl+Alt+J -> 7 (ReSharper 2.x/IDEA scheme)

Claque answered 4/5, 2010 at 23:58 Comment(0)
W
22

How about:

Ctrl-X, {, Ctrl-V, }

You could even bind that to a macro.

Whereabouts answered 5/5, 2010 at 0:16 Comment(2)
Reasonable, but doing this puts the selected text on the clipboard.Czech
I like this! It's even simpler in the latest VS versions which autocomplete the second bracket for you... Ctrl-X, {, Ctrl-VUnexperienced
T
3

In VS2015 there is an experimental feature that supports it by selecting the text and typing in }.

See here how to enable.

Terryn answered 12/4, 2016 at 18:46 Comment(2)
and VS just crashed for the first time in days, minutes after I enabled the experimental features in C++ editor options... even though I have no CPP code in my sln.Radioelement
it does work for me on my C++ solution, which is quite complex, however it more likely depends of what you select to surround.Terryn
F
1

Make your own custom code snippet for doing that. You can use snippy to create your own http://blogs.msdn.com/gusperez/articles/93681.aspx or just use an XML editor to create one.

Put the file in My Documents\Visual Studio XXXX\Code Snippets\C#\My Code Snippets

Fitful answered 4/5, 2010 at 23:18 Comment(0)
A
1

To complete Ray Vega's answer, for those using Resharper, I figured out you can associate a shortcut to Resharper commands.

Just do the following (I am using VS 2010):

go to Tools->Options

In the listbox, extend Environment and click on Keyboard.

In the field under "Show commands containing:" enter "resharper.resharper_surroundwith"

In the field under "Press shortcut keys" enter your shortcut (eg: I choose Ctrl+R,Ctrl+S) and click Assign and then Ok.

That's it. you can select your code, and type that shorcut to view all Resharper SurroundWith commands. Just enter 7 to put braces.

Ai answered 15/3, 2013 at 18:53 Comment(1)
This is not an answer to the original question. It should be a comment to the referenced answer, or extend the referenced answer with an edit.Auguste
S
0

Edit: This turns out to be part of DxCore, from DevExpress. Leaving here so others notice, but basically I was wrong wrong wrong. To make this particular menu go away you disable it in the 'add ins' dialog; unloading devexpress from their own menu just unloads CodeRush/Refactor, not the base support libraries.

The is (not!) a built in way to do it. I don't know if you can bind a key to it or not. Also, this embed doesn't do anything if you only select one line, so it probably won't work right if your stuff is on one line after the "if".

  1. Select the block
  2. Right Click
  3. Choose "Embed Selection"
  4. Choose "Block {}"

Note: I have DexExpress installed, but this menu is there even when it isn't loaded, and I could swear it is there even when it isn't installed. However, if I am mistaken...

This honestly seems like something that would be best to ask r# for, a user contrib perhaps?

Sieracki answered 4/5, 2010 at 23:23 Comment(3)
This option does not seem to be in my version of VS2008, but maybe I have something configured differently. It does have a "Surround with..." option, but no curly brace in there.Ralf
It turns out it is part of DxCore, the support framework for DevExpress. Noticed it when I went to see if there was some other mystery addon that I forgot about. Even when you disable the main product this still stays, which explains why I can't find it in the menu.Sieracki
@Mark that is the same thing I am seeing. Seems like I remember seeing this when I was using DevExpress' CodeRush.Smukler
C
0

For new comers in 2022,

Until this extension is available in the market, you have to clone the repo, build it and install it.

source

Cayser answered 19/10, 2022 at 20:39 Comment(0)
A
-1

You can wrap a code block with braces by

  1. Highlight the code block
  2. Ctrl e -> Ctrl u
  3. select option 7

I know this is an old question but I hope it helps someone

Ref: Wrapping multiple statements in braces

Aman answered 1/4, 2016 at 2:36 Comment(1)
Note this applies to Visual Studio with Resharper. It won't work without Resharper.Ingram

© 2022 - 2024 — McMap. All rights reserved.