When working in code, it can be useful to quickly determine where a matching brace is located. This tip show how to enable brace matching, how to navigate between braces, and how to change the default color highlight for better visibility.
There are a number of useful utilities that can help with this, like CodeRush and ReSharper. And there are some other code extensions available in Visual Studio. Today, however, I'm talking about what's included in the generic Visual Studio. I'll show you an example in this C# file. I'm editing the code, and I'm looking at the code on the bottom of this method. I'm interested in the curly braces on line 29 and 28. To activate the brace matching, I need to move my cursor to the curly brace. I can do that with my mouse, or I can use the keyboard.
I'll use the keyboard first. I'll press the up arrow. And then I'll move to the right. When I'm on the outside edge of the curly brace, you see there is a gray rectangle that appears superimposed over the curly brace. That's the matching brace rectangle. If I press the space bar and move away from the edge of the curly brace, you'll see that that matching brace rectangle disappears. So I have to be right next to the curly brace, like this. Now if I look up on line 13, I can see that there's a curly brace up there.
And it has the same matching rectangle. So that tells me that the curly braces on line 13 and 29 are a pair. Now, I said you could use your mouse too, so I'll use my mouse cursor and move to this curly brace on line 28. And you'll see that I get the matching brace on line 26. Now, when I move on the inside of this starting curly brace, the highlight disappears. That's because you have to be on the outside edge. So, with the starting curly brace, you need to be to the left of the curly brace. The curly brace matching rectangle is light gray, and I think that's hard to see.
It's the default. It's subtle. And I like to enhance it a bit. So let me show you how to change those settings in the Visual Studios Tools Options window. So go to Environment and then Fonts and Colors. And then scroll down here to where you have this Brace Matching. Now, there's three items here. We're looking for this one here, this Brace Matching. And I will set the background color to an orange color. Now, you pick your own colors. I typically use a more subtle choice than I'm going to show you today.
I'll dial this back to a light orange color, like about like that. Then I'll also change the item foreground to a dark red color. Now, when I return back to Visual Studio and do my brace matching, it's a lot more apparent which brace is matching because of the red color. And notice that the brace itself is still black. The dark red color that was referred to in the settings is the rectangle's outline.
So that's your basics. That's brace matching. The next step is to talk about what happens when you want to move between braces. There's a keyboard shortcut you can use. You need to hold down the Control key and then tap the square brace key on your keyboard. Now, there's two square brace keys on your keyboard. We're talking about the right square brace. So hold down the Control key, tap the right square brace. And you'll see that my cursor moved to line 29. Now, do that again. Hold down Control, tap the same square brace key, and you'll see that it moves back up to line 13.
So this is the real tip here, is not only can you see the matching brace, but you can move between them. Now, in this example I can see all my code on the same page, so it might not be that useful. But in a larger document, where I can't see the beginning of the method or the beginning of the statement, this is helpful. I also like to point out that the term brace doesn't just mean curly brace. Another type of brace, as far as Visual Studio is concerned, is the square brace.
So if I move my cursor here, you'll see that I get the same brace matching highlight. And I can use the same keystroke to move between the beginning square brace and the ending square brace, like this. And also, this works for parentheses. Let me show you on this set of parentheses here. So I've got this if statement. I'm on the end parenthesis. And then I'll use the Control + square brace to move to the beginning of the parentheses. So the takeaway from this is, use the brace matching to find where your braces are.
Change the colors to a more useful color if you find it too subtle. And the third thing is, you can move between your matching braces by using the keyboard shortcut.
Skill Level Intermediate
Q: Why can't I earn a Certificate of Completion for this course?
A: We publish a new tutorial or tutorials for this course on a regular basis. We are unable to offer a Certificate of Completion because it is an ever-evolving course that is not designed to be completed. Check back often for new movies.