Is there a shortcut to surround a given (selected) code block with curly braces?
VS 2015 + R# 2016.1
Braces improve the uniformity and readability of code. More important, when inserting an additional statement into a body containing only a single statement, it is easy to forget to add braces because the indentation gives strong (but misleading) guidance to the structure.
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 .
What Does Bracket Mean? Brackets, or braces, are a syntactic construct in many programming languages. They take the forms of "[]", "()", "{}" or "<>." They are typically used to denote programming language constructs such as blocks, function calls or array subscripts. Brackets are also known as braces.
How are curly brackets used? Curly brackets are commonly used in programming languages such as C, Java, Perl, and PHP to enclose groups of statements or blocks of code.
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 :
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>
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
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With