68

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?
}
2

9 Answers 9

48

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.

0
30

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

5
  • 2
    You just changed my life. And here I thought the breadcrumb bar was silly. Commented Nov 19, 2021 at 17:38
  • `Ctrl-Shift-\` takes you directly to the matching bracket (found via ctrl-shift-p and searching 'go to') Commented Nov 28, 2023 at 16:37
  • 1
    the combination doesn't exist on my installation. what is the name of the command?
    – birgersp
    Commented Apr 19 at 7:42
  • 1
    @birgersp breadcrumbs.focus
    – Jonas
    Commented Jul 29 at 19:52
  • This is the way !
    – gberth
    Commented 2 days ago
25

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.

3
  • So are you saying that you should "go to the beginning to collapse the function" in order to be able to.....go to the beginning of the function? I liked the first solution though.
    – Andrew
    Commented Feb 5, 2020 at 20:21
  • @Andrew, the "Note" part in my answer was referring to how to get to the bottom of a large function easily.
    – Chief A
    Commented Feb 9, 2020 at 14:28
  • 1
    Oh, so you mean "immediately after the function" and not "immediately enclosed by the function". :)
    – Andrew
    Commented Feb 9, 2020 at 15:36
21

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. enter image description here

If shortcut doesn't work for you

1
  • In VS 15.9.4 Ctr+Alt+Up highlights the Project selector
    – Chief A
    Commented Jan 4, 2019 at 11:38
8

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.

2
  • 10
    In this case you can also use keyboard shortcut Ctrl+M, Ctrl+M. Or (if you are using a different keyboard scheme) look the correct shortcut for the command Edit.ToggleOutliningExpansion
    – Max
    Commented Jan 13, 2017 at 22:48
  • 4
    Double-clicking the line closes the block, not the function.
    – Chief A
    Commented Jan 4, 2019 at 10:35
5

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().

0
4

I use ctrl + Shift + o and Enter.

ctrl + shift + o opens up the "Go to symbol..." dropdown at the top which has the current function selected when it opens up and Enter which takes me to the beginning of the current function.

For full and up-to-date shortcuts refer to the official Visual Studio Code keyboard shortcuts here: https://code.visualstudio.com/shortcuts/keyboard-shortcuts-windows.pdf

Tip: ctrl + k, ctrl + r would open up the keyboard-shortcuts page in a browser window.

1
  • And ctrl + cmd + o on a mac Commented May 23, 2023 at 13:29
1

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.

0

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!

2
  • 1
    just to add . click anywhere ->see the name of the method in dropdown list on top right-> press the dropdownlist and choose the method again and thats it. Commented Jul 16, 2019 at 9:32
  • Just clicking on that dropdown and pressing ENTER is enough, without copying and pasting anything. Or you can replace that ENTER with moving the mouse a few pixels below to select the first element (the current method) and clicking.
    – Andrew
    Commented Feb 5, 2020 at 20:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.