1

Is there any keyboard shortcut I can use to go up to start of, for example, foreach loop which I'm editing at the moment? And maybe there is also a shortcut to go to the end of the loop?

Maybe there is something like that when I'm using Resharper, if not in vanilla Visual Studio?

I'm not writing about debugging! Just writing the code.

Sorry, but I can't find anything about this, but it seems like some basic functionality IDE should have...

Ap0st0l
  • 515
  • 4
  • 26
  • If you're looking for such a thing, I'm going to suggest that your methods may contain too many lines of code... – Matthew Watson Feb 26 '19 at 10:19
  • Firstly I wasn't the one writing them... Secondly I'm also editing large XSLT files, which can't be split. – Ap0st0l Feb 26 '19 at 10:20
  • Possible duplicate of https://stackoverflow.com/questions/1501921/go-to-matching-brace-in-visual-studio – Mangs Feb 26 '19 at 10:42
  • @Mangs I'm curious when ppl will notice that duplicates are not evil, especially when question is asked differently. Probably at the moment when Internet will be full of outdated answers and old questions... – Ap0st0l Feb 26 '19 at 10:52
  • @Ap0st0l Duplicates are not evil, but a direct duplicate is just pointless. And if you look at the answers on this question you can see that they are quiet irrelevant. It sure is good to know how to use bookmarks and how to refactor your code to shorter methods but that is probably not the reason ppl end up here. – Mangs Feb 26 '19 at 13:54

4 Answers4

3

You can use Ctrl+]: it

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

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Piotr Moreń
  • 126
  • 5
  • Do you know how to do that on a non-US (e.g. German) keyboard? The `]` is only accesible with `Alt+Gr`, which doesn't work as a shortcut. – René Vogt Feb 26 '19 at 10:33
  • 1
    @RenéVogt you can remap it in the Visual Studio keyboard options page - the command name is `Edit.GotoBrace` – stuartd Feb 26 '19 at 10:36
2

You can certainly jump between the braces. If you have your cursor either side of one of the braces (at the start or end of your loop) use CTRL + ] to jump to the other one. This will work on any set of braces, not just loops.

Owen Pauling
  • 11,349
  • 20
  • 53
  • 64
0

Do you mean you want to move the cursor to the starting/ending bracket? I don't know of one that does this, although you can collapse the loop to see both what happens before and after at the same time.

although, to be fair, if your loop has so many lines that you can't see both brackets at the same time, it might be a good idea to make the code inside the brackets shorter (for example, with an extra method:

foreach (string entry in entryList)
{
   AnalyseValue(entry);
}
ThisIsMe
  • 274
  • 1
  • 5
0

You can create bookmarks on specific lines of code (Ctrl+K, Ctrl+K) and navigate to your next book or previous bookmark (Next Bookmark => Ctrl+K, Ctrl+N) (Previous Bookmark => Ctrl+P)

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Tiago Silva
  • 2,299
  • 14
  • 18
  • 2
    Bookmarks are very useful, especially when you notice something but don't want to look at it right now, but this doesn't answer the question.. – stuartd Feb 26 '19 at 10:37