Guimo
Guimo

Reputation: 732

How to delete matching brackets in VS Code?

I know you can jump between matching brackets with Ctrl + Shift + \. I would like to place the cursor right after a bracket and delete both that bracket and its matching one as easily as possible.

Since with Alt + Click you can have multiple selections, I was looking for something similar to: Ctrl + Shift + Alt + \ for placing another cursor on the matching bracket and then deleting both with a single backspace.

Is there any shortcut deleting a pair of matching brackets/parenthesis?

Upvotes: 40

Views: 19752

Answers (4)

neaumusic
neaumusic

Reputation: 10464

In settings, Editor: Auto Closing Delete is what I was looking for -- changing this from auto to always did the trick.

EDIT: this only works for adjacent brackets

Upvotes: 0

Mark
Mark

Reputation: 181248

In the v1.77 Insiders Build now (and probably in Stable v1.77 early April 2023) is a new command:

Remove Brackets
editor.action.removeBrackets

By default bound to Ctrl+Alt+Backspace on Windows and Cmd + Opt + Backspace on Mac.

This command will remove matching brackets that surround the cursor position. So the cursor can be anywhere - it doesn't have to be right next to one of the brackets - in code or text that has a surrounding bracket. (It doesn't appear to reformat after the bracket deletions though).

Upvotes: 29

Synthetica
Synthetica

Reputation: 940

There is an extension called Bracketeer that does what you want.

First, install with

Ctrl-p, then ext install pustelto.bracketeer, followed by an enter.

You can then add the following to your keybindings.json

  {
    "key": "ctrl+alt+backspace",
    "command": "bracketeer.removeBrackets"
  },

You can then use ctrl-alt-backspace to remove matching brackets.

Upvotes: 45

eldix_
eldix_

Reputation: 127

This works for (), {} and []

Sub DeleteMatchingBrace()
Dim sel As TextSelection = DTE.ActiveDocument.Selection
Dim ap As VirtualPoint = sel.ActivePoint

If (sel.Text() <> "") Then Exit Sub
' reposition
DTE.ExecuteCommand("Edit.GoToBrace") : DTE.ExecuteCommand("Edit.GoToBrace") 

If (ap.DisplayColumn <= ap.LineLength) Then sel.CharRight(True)

Dim c As String = sel.Text
Dim isRight As Boolean = False
If (c <> "(" And c <> "[" And c <> "{") Then
    sel.CharLeft(True, 1 + IIf(c = "", 0, 1))
    c = sel.Text
    sel.CharRight()
    If (c <> ")" And c <> "]" And c <> "}") Then Exit Sub
    isRight = True
End If

Dim line = ap.Line
Dim pos = ap.DisplayColumn
DTE.ExecuteCommand("Edit.GoToBrace")
If (isRight) Then sel.CharRight(True) Else sel.CharLeft(True)

sel.Text = ""
If (isRight And line = ap.Line) Then pos = pos - 1
sel.MoveToDisplayColumn(line, pos)
sel.CharLeft(True)
sel.Text = ""
End Sub

Then add a shortcut to this macro in VS

Upvotes: -5

Related Questions