58

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?
}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
wootscootinboogie
  • 8,461
  • 33
  • 112
  • 197
  • 1
    See that: http://stackoverflow.com/a/9748154/1566267 And also: http://stackoverflow.com/a/13271871/1566267 – John_West Jan 06 '15 at 18:56
  • The shortcut `Edit.PreviousMethod` will accomplish that. As will the shortcut `Edit.RainbowPrevious` in the Viasfora extension. – ingredient_15939 Apr 23 '19 at 16:22

9 Answers9

41

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.

Anthony
  • 806
  • 7
  • 14
22

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.

Chief A
  • 510
  • 10
  • 24
  • 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 Feb 05 '20 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 Feb 09 '20 at 14:28
  • 1
    Oh, so you mean "immediately after the function" and not "immediately enclosed by the function". :) – Andrew Feb 09 '20 at 15:36
20

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

Stas Boyarincev
  • 3,690
  • 23
  • 23
20

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

Fabs
  • 161
  • 1
  • 9
Berthier Lemieux
  • 3,785
  • 1
  • 25
  • 25
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.

Tsagana Nokhaeva
  • 630
  • 1
  • 6
  • 25
Adam Jonsson
  • 2,064
  • 1
  • 13
  • 9
  • 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 Jan 13 '17 at 22:48
  • 4
    Double-clicking the line closes the block, not the function. – Chief A Jan 04 '19 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().

KulaGGin
  • 943
  • 2
  • 12
  • 27
3

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.

Bala TJ
  • 700
  • 6
  • 10
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!

  • 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. – Yinon Dotan Jul 16 '19 at 09: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 Feb 05 '20 at 20:25
0

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.

haku
  • 4,105
  • 7
  • 38
  • 63