ChatGPT解决这个技术问题 Extra ChatGPT

Go to Matching Brace in Visual Studio?

Is there a way in Visual Studio 2008 to go from a closing brace to its opening brace? I've found a fair amount of stuff about highlighting the brace, but nothing about moving the cursor to it.

(VB.NET version of this Question: Keyboard shortcut for Jumping between "If/End If")

incredibly, this seems to not work (2019) for .js files on Mac Visual Studio. WTH ?!
@Fattie, It doesn't work in Windows 10 for .cs files either. I am pretty confident this is an across-the-board thing for VS 2019 so I made a new question: stackoverflow.com/q/59183422/1739000
Instead of trying to CTRL+F this page for whatever language keyboard you are using, use this answer to figure it out in Visual Studio settings.
Since you might be here about VS Code, here is the question for that instead: stackoverflow.com/questions/30097634/…

G
General Grievance

I found this for you: Jump between braces in Visual Studio:

Put your cursor before or after the brace (your choice) and then press CTRL + ]. It works with parentheses ( ), brackets [ ] and braces { }. From now on you don’t need to play Where’s Waldo? to find that brace.

With the above shortcut, you can also hold SHIFT to select.

On MacOS, use CMD + SHIFT + \ .


Just for information, on a Norwegian keyboard the short-cut for this is Ctrl+Å (same as for Danish keyboard mentioned in an answer below).
On Belgian azerty it seems to be ctrl+^, which is, oddly, on the key which also has the [ bracket (which needs AltGr to be typed).
Icelandic keyboard CTRL + Ð
For Czech QWERTZ keyboard it's CTRL + ). Exactly same position, where English keyboard has ] key. Just above Enter.
If you're working in a .sql file, CTRL + ] will toggle between BEGIN and END pairs. (VS 2015)
P
Peter Mortensen

I use Visual Studio 2008, and you can customize what you want this shortcut to be.

Click menu Tools -> Options -> Environment -> Keyboard. Then look for Edit.GotoBrace.

This will tell you what key combination is currently assigned for this. I think you can change this if you want, but it's useful if the Ctrl + ] doesn't work.


This is very useful to find keybindings for other languages' keyboards.
Yes, we should always respond to command questions with the command name in case the keyboard is different or the default shortcut has been removed. Also check out Edit.GotoBraceExtend, if you map Ctrl+[something] to Edit.GotoBrace, you'll probably want to map Ctrl+Shift+[something] to Edit.GotoBraceExtend.
This was useful for me because it wasn't set for me at all in VS 2017.
A
Ahmad Mageed

Use CTRL + ] to switch between them. Place the cursor at one of the braces when using it.


P
Peter Mortensen

Note: It also works for #if / #elif / #endif matching. The caret must be on the #.


P
Peter Mortensen

If for some reason this is NOT working for you, something may have messed up your keyboard bindings (it didn't work for me). You can re-enable the binding easy enough though - at least so I thought:

I tried this procedure:

Go to menu Tools -> Options -> Environment -> Keyboard

Scroll to, or search for the command Edit.GotoBrace

Assign the desired shortcut (mine was empty, so I put in CTRL + ])

Be sure to click the "Assign Button"

I tried it, and it still didn't work. I restarted Visual Studio, and it still didn't work - well it ONLY worked for .cs files, but I need it to work for .vb files and text files, and...well ALL files!


For VB files, use CTRL + SHIFT + UP/DOWN (ref: stackoverflow.com/a/7895072/740639)
M
Matthieu Rouget

On my French keyboard, it's CTRL + ^.


B
Baldewin

On a German keyboard it's Ctrl + ´.


P
Peter Mortensen

On a Spanish keyboard it is CTRL + ¿ (or CTRL + ¡).


10 minutes to discover WTF was ¡, as written into VS2008 options menu. Thanks!
P
Peter Mortensen

On my Danish keyboard it's CTRL + Å.


P
Peter Mortensen

On a Turkish keyboard, it is Ctrl + ü.


P
Peter Mortensen

And Ctrl + Shift + ] will select all of the text.


O
Ola Karlsson

For completeness sake, on a Swedish keyboard it's CTRL + å .

Also, I guess logical, but worth mentioning CTRL + shift + å (for capital Å), selects everything inside the braces and goes to the matching one.


P
Peter Mortensen

On my Portuguese keyboard and SO with EN VS, it's CTRL + « to navigate to matching brace and CTRL + SHIFT + « if you intend to select the inner code.


j
j0k

On my Italian keyboard, it's CTRL + ^.


P
Peter Mortensen

On my pt-BR (Brazilian Portuguese) keyboard it is actually CTRL + [.


S
Simon Dugré

On my Slovenian keyboard it is ALT + Đ


It's CTRL + đ by default
C
Community

On a Mac use command+shift+\.

Source: a comment on this answer: https://stackoverflow.com/a/37877082/3345085. Tested in Visual Studio Code version 1.10.2.


Note: this is for Visual Studio Code. The question is about Visual Studio. There is a separate question for VS Code.
T
The Red Pea

For Visual Studio Code (as seen in their documentation), use Ctrl+Shift+\.

The setting can be found in:

File/Preferences/Keyboard Shortcut

I am using Visual Studio Code 1.8.0 . Note Visual Studio Code may behave differently for international keyboards (as seen in this answer re: German keyboard)

Hope this helps someone.


Note: this is for Visual Studio Code. The question is about Visual Studio. There is a separate question for VS Code.
J
Jasper

On my Dutch (Belgian) keyboard, it's CTRL + ^.


a
animuson

Goto Tools > Options > Environment > Fonts and Colors, select the "Brace Matching (Rectangle)" and change the "Item Background" to e.g. Yellow. This worked for the C# parentheses () {} and [].


Yes helpful & 'Display Name' text box of Fonts & Colors is where Brace Matching Rectangle is located.
If you have the dark theme like I do, cyan instead of yellow will also make the bracket itself more visible.
B
Bruno Bronosky

Details that can benefit everyone (Linux/Win/Mac)

The command in the keyboard shortcuts menu/editor is editor.action.jumpToBracket there you can set it to whatever you like. There is also one called editor.action.selectToBracket which has no shortcut by default (at least on Mac).

Etc.

On the Mac editor.action.jumpToBracket starts out as Cmd+Shift+\ and I changed it to Ctrl+] to be in line with what others say here. I did so in the hopes that I could use Ctrl+Shift+] to "Extend selection to matching bracket". That is what lead me to discover the details above. I set editor.action.selectToBracket to Ctrl+Shift+] and got exactly the behavior I wanted.


P
Peter Mortensen

On a Hungarian keyboard it is Ctrl + ú.


P
Peter Mortensen

A bit relevant, but for HTML tags: (since there is no built-in solution, Ctrl + J doesn't work for HTML tags : )

Here is the answer as a macro which I've built which does it (toggle), including go to focus:

Here is the demo:

https://i.stack.imgur.com/pb6Iz.gif

And here is the code. Enjoy!

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE90a
Imports EnvDTE100
Imports System.Diagnostics
Imports System.Windows.Forms

Public Module Module2
    Sub beginToEnd()

        'Place the cursor somewhere in the beginning tag, run the macro, to select from beginning to end tag

        DTE.ActiveDocument.Selection.SelectLine()
        Dim objSel As TextSelection = DTE.ActiveDocument.Selection
        Dim topPoint As TextPoint = objSel.TopPoint
        Dim lTopLine As Long = topPoint.Line
        objSel.GotoLine(lTopLine, False)
        '  DTE.ActiveDocument.Selection.StartOfLine()
        DTE.ActiveDocument.Selection.SelectLine()
        Dim line1 As String = DTE.ActiveDocument.Selection.Text()

        If InStr(line1, "/") Then

            ' MsgBox(line1)
            DTE.ExecuteCommand("Edit.ToggleOutliningExpansion")
            DTE.ActiveDocument.Selection.EndOfLine()
            DTE.ActiveDocument.Selection.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstText, True)
            objSel.GotoLine(lTopLine, False)
            DTE.ExecuteCommand("Edit.ToggleOutliningExpansion")
            DTE.ExecuteCommand("Edit.ToggleOutliningExpansion")
        Else
            DTE.ExecuteCommand("Edit.ToggleOutliningExpansion")
            DTE.ActiveDocument.Selection.EndOfLine(False)
            DTE.ExecuteCommand("Edit.ToggleOutliningExpansion")
        End If

        DTE.ActiveDocument.Selection.SelectLine()
        Dim line2 As String = DTE.ActiveDocument.Selection.Text()
        Dim objSel3 As TextSelection = DTE.ActiveDocument.Selection
        Dim topPoint3 As TextPoint = objSel3.TopPoint
        Dim lTopLine3 As Long = topPoint3.Line
        objSel.GotoLine(lTopLine3, False)
        DTE.ActiveDocument.Selection.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstText, False)
    End Sub
End Module

The standard US mapping of + ], works for me in HTML and CSS as well. Both tags get highlighted too.
M
Markus

In Visual Studio Code on german keyboard it's ctrl+shift+^

But you have to open a file with correct extension - it's not working in new unsaved files for example.


Note: this is for Visual Studio Code. The question is about Visual Studio. There is a separate question for VS Code.
F
Fabio Salvalai

On the Swiss-French keyboard : use CTRL + SHIFT + ^


D
David Oliván Ubieto

On Spanish (Spain) keyboard with VS2012 is Ctrl + ¡ as stated by @Keith but if you use Ctrl + ¿ (typed as Ctrl + Shift + ¡) then goes to Matching Brace plus selects all the code within the two braces and then you can't go again to the other brace.


S
SunbeamRapier

June 2021

On a Mac running Windows 10 under parallels for Visual Studio 2019 and c# and with US keyboard the solution which worked for me was to edit the shortcut in" Tools Options Keyboard Edit.GoToBrace

On my system that shortcut was blank...

Press the Ctl key and the ] key and save the shortcut