Is there a keyboard shortcut in Visual Studio 2010 (I'm using ReSharper 6.1 also) that will allow me to surround a selected block of text with curly braces? I tried "Surround With..." (Ctrl+K, Ctrl+S), but I didn't see an option in the list to choose curly braces as the surrounding element. The common use case for this is that I'll have an if-statement like the following:
if (conditional)
statement1;
// the rest of the program
I'll realize that there are some additional tasks that need to be performed inside the if-statement and I add them:
if (conditional)
statement1;
statement2;
statement3;
// the rest of the program
Then, I remember that I need to wrap all the statements in curly braces and the code should really look like this:
if (conditional)
{
statement1;
statement2;
statement3;
}
// the rest of the program
What I'd like to do is just select the three statements and then hit a shortcut key to wrap them in curly braces. What I actually end up doing is moving the cursor to the beginning of the line after the conditional, then typing a { character, then deleting the } character that ReSharper (unhelpfully) automatically inserts immediately after the {, then moving the cursor down to end of the last statement of the block and entering } to complete the block.
Select rows of code.
Press Ctrl E-U (Surround with template) (or Ctrl Alt J for Intelli J).
Select option 7: { }.
Works for me.
Related
Is there any convenient way to do all these things at once in Xcode?
Take a selected block of text and enclose it in a set of curly braces, each on their own line, with proper indentation like this:
Before:
idea.ponder()
doSomething()
After selecting the 2 lines and hitting a shortcut:
{
idea.ponder()
doSomething()
}
… preferably with the cursor positioned just before the opening brace.
That way I could just, for instance, type withAnimation or DispatchQueue.main.async or if !tooSleepy and be on my way.
I find myself doing this sort of thing manually so often, and I've never seen a convenient Xcode shortcut. (Though there are shortcuts for indenting, for wrapping in braces without adding newlines, etc.) Am I missing something?
thanks!
On my machine, selecting the lines and typing a left curly brace does exactly what you just said. Example:
self.contentView.layer.cornerRadius = 8
self.contentView.backgroundColor = .blue
I select both lines (triple-click-and-drag) and hit { key, and I get
{
self.contentView.layer.cornerRadius = 8
self.contentView.backgroundColor = .blue
}
To get the cursor before the first curly brace, choose Editor > Selection > Balance Delimiters (you can give that a shortcut) and then left-arrow.
An alternative approach might be: select and cut the lines, use code completion to insert the desired construct, then paste the lines back in.
In Visual Studio 2019, it likes to try to guess where it should put the semicolon when you hit it. Often it will jump to a completely different line or insert newlines. Every time it does anything besides append it as the next character, this is extremely disruptive, and I have to go back and fix what it broke. This is akin to the disruptive "Automatic brace completion" which always puts braces where I don't want them, but can be turned off. I can't find anywhere to turn off the semicolon behavior. Is there any way to turn this feature off?
Most of the time when the semicolon misbehaves, it's because I hit it by mistake, but rather than hitting backspace, I now have a bigger mess to clean up. And I've never had a situation where it did something extra that I wanted it to do.
Some examples, with * being the cursor location:
// between a ) and ;
Foo()*;
// inserts an extra space
Foo();* ;
// before an existing semicolon:
return null*;
// moves the old semicolon to the next line:
return null;*
;
// pressing semicolon in the middle of a multi-line function call:
string path = Path.Combine(
"C:\\",
"b"*
"filename.txt");
// moves the cursor to the end of the block and inserts a "; " before ";":
string path = Path.Combine(
"C:\\",
"b"
"filename.txt");* ;
Visual Studio 16.10 adds a new configuration option to control this:
Text Editor -> C# -> Intellisense -> Automatically complete statements on semicolon
With that checked, I get the problematic behavior described in the question. When I uncheck that option, it works as it did before.
In Visual Studio
while i am typing a string inside braces
what is the hotkey to go to ending bracket ] without pressing end
item["i am typing here..."] //i want an hotkey to jump here
if it's not possible
how can i jump out of current box which i am typing in?
namespace{
class{
method(){
lambda->({(jump out of this box)});
}
}
}
according to this https://www.dofactory.com/reference/visual-studio-shortcuts you should use end button to go to ending bracket and you can see all of hotkeys in this link.
You are looking for Ctrl-]. From the documentation:
Moves the cursor to the matching brace in the document. If the cursor is on an opening brace, this will move to the corresponding closing
brace and vice versa
At the bottom line - it will bring you to the end of your current braces-scope (or to the beginning of it if you are on the closing brace )
I did this trick after starting to type inside quotes ("")
press tab then press ]
It's much better than pressing end
Sometimes this won't work and will print /t character
I don't know how to deal with that
Still, there should be a better answer to this!
I know that Ctrl+} will take you to the corresponding brace in Visual Studio, but say I'm in the middle of a gigantic function and I don't know where the top or the bottom is, is there a shortcut to get directly to the function declaration?
void function()
{
//so many lines of code
//can't see the top or the bottom curly brace
//can i get to the top of the function with a shortcut?
}
I have a fresh install of VS2017. As of 15.9.1, the default for me is Alt+Shift+[.
This is the shortcut for EditorContextMenus.Navigate.GoToContainingBlock. So you may have to execute this shortcut multiple times if you are a few block layers deep, but it'll get you where you want to go.
Alt+Ctrl+UP,Tab,Tab,Enter
This sequence will move you through Project selctor > Scope selector > Function selector > Current Function.
Ctrl+M,Ctrl+M
This sequence will toggle between collapse/expand current block.
Place cursor at any line that is immediately enclosed by the function. Collapse. Place cursor at the end of the collapsed function, i.e after { ... }. Expand the function to get to its last brace.
Note:
If you have difficulty in finding a line immediately enclosed by the function(for example, when the function has lots of nested blocks), you can always goto the beginning to collapse the function.
Update
With last updates Visual Studio, now default keyboard shortcut for EditorContextMenus.Navigate.GoToContainingBlock is Shift+Alt+[
Old Answer:
Visual Studio 2017 version 15.8.0 comes with a new shortcut Ctrl + Alt + UpArrow - Go to Enclosing Block.
Go to Enclosing Block (Ctrl + Alt + UpArrow) allows you to quickly
navigate up to the beginning of the enclosing code block.
Source
This command allows also move to function declaration if you are inside function.
If shortcut doesn't work for you
For the VSCode lovers, this key combination will bring you to the top of the function:
Ctrl-Shift-.
followed by ENTER
and for MAC users:
Cmd-Shift-.
followed by ENTER
I usually double press the white line that is located left of the code.
It closes the function but it also takes you to the declaration of the function.
You can do it with Macros for Visual Studio extension.
Here's the code for macros:
// BeginningOfFunction moves the caret to the beginning of the containing definition.
var textSelection = dte.ActiveDocument.Selection;
// Define Visual Studio constants
var vsCMElementFunction = 2;
var codeElement = textSelection.ActivePoint.CodeElement(vsCMElementFunction);
if (codeElement != null)
{
textSelection.MoveToPoint(codeElement.GetStartPoint());
dte.ActiveDocument.Activate();
}
It is one of the sample macros of the extension. Edited it a little, because for some reason sample didn't work for me. You can get to the end of the function by changing codeElement.GetStartPoint() to codeElement.GetEndPoint().
I found one trick in visual studio:
Place the cursor on the empty to get the context (name of the function), copy the name of the function, then click the drop down arrow where all functions will be listed, paste the function name, enter. Then you are at the beginning of that function!
Another alternative would be to use Edit.PreviousMethod. I prefer this option because even if your cursor lies in multiple nested block, you can get to the method definition in single keystroke! I have mapped Edit.PreviousMethod to ctrl + alt + , and Edit.NextMethod to ctrl + alt + . but you can set it to whatever you prefer.
To setup key binding, goto Tools.Options.Environment.Keyboard, then in Show Commands Containing textbox type edit.previousmethod, set focus on Press Shortcut Keys textbox and press the key combination you want, the hit Assign. Repeat for edit.nextmethod, then Ok.
I just started using Visual Studio 2013. I liked the auto brace completion feature. But in C#, when I press enter after entering {, no extra line to write our code is formed, like in eclipse. Instead the cursor will be at the left side of the }. Did anybody notice that? Is there a way to fix it.
When I press Enter key after putting brace, the editor is like this:
class Example {
|}
(The cursor is where the pipeline character is..)
EDIT:
I just found that in Visual C++ 2013, the braces work as expected.
class Example {
|
}
(The cursor is where the pipeline character is..)
Check within Options on the Text Editor/All Langauges/General page. Select "Automatic brace completion."
By the way in 2013 you can search for words like "brace" at the top of the treeview control in Options to find all the pages were "brace" is found.
Answer taken from here.
Try typing
class
then press Tab.
Same goes for interface, if blocks, etc.
If you want to write a property, type
prop
then press Tab.
You can press Ctrl-Enter and it will reposition the closing brace as you expected. Because Ctrl-Enter makes a new line above the current line.