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
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
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.
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
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!
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.