Automatically Delete Corresponding Bracket in TeXstudio

Let me start off by saying that I'm not looking for how to disable bracket completion. In fact, when I tried searching for what I'm about to ask, that kind of information was the only thing that kept coming up...

What I'm looking for is something akin to how a typical programming compiler will handle bracket auto-completion. For example, I program in Python with Spyder a lot, and when I accidentally open any sort of ([{ bracket, although initially the bracket is auto-completed on the other side with a )]}, if I were to delete that bracket, it also automatically removes the corresponding bracket. In TeXstudio, however, hitting backspace on the accidental bracket leaves the "closing" bracket in the file.

Although it's not a difficult task per se to delete the other bracket manually when this happens, 50 pages of latex in and I'm starting to think that someone smarter than me must have already thought of a way to solve this. Ideas?

• Welcome to TeX.SX! – Bobyandbob Aug 4 '17 at 21:23
• I would love to see a good answer to this. It is really easy to get used to this convenience feature in IDEs like RStudio. – tmastny Sep 28 '17 at 19:16
• @tmastny See my answer. – Troy Dec 29 '17 at 15:12

As of time of writing, I do not think there is an option to delete corresponding brackets automatically (which is a shame :o). There is a way to do this via a macro/user script in TeXstudio, but is slightly more involved, so I'll lay it out in a step-by-step manner.

User script for "Automatically deleting corresponding brackets"

Notes on use: The following script deletes both brackets in (), {} or [] automatically when (i) the cursor is in between the open and close brackets and (ii) the Backspace key is pressed. Such situations in TeXstudio arise most frequently when "Auto Complete Parenthesis" option is turned on. (See Gif demonstration below.)

1. From the main toolbar, go to Macros -> Edit Macros. Click Add + on the bottom left corner of the pop-up window to add a new user script. Paste the following in the large box that says LaTeX content on the right side of the pop-up window:

%SCRIPT
previousChar = function() {return String.fromCharCode(cursor.previousChar())};
nextChar = function() {return String.fromCharCode(cursor.nextChar())};
var Obrackets = ['(','[','{'];
var Cbrackets = [')',']','}'];
var Omatch = Obrackets.indexOf(previousChar());
var Cmatch = Cbrackets.indexOf(nextChar());
if(Omatch>=0 && Cmatch==Omatch){
cursor.deleteChar();
}
if(cursor.hasSelection()){
cursor.removeSelectedText();
} else {
cursor.deletePreviousChar();
}


Give it a name if you like (for ease of finding it in Step 2). Close out of the macros window.

1. From the main toolbar, go to Options -> Configure TeXstudio, and navigate to:

Shortcuts -> Menus -> Macros


and find the macro that you created in the first step. Change the Current shortcut for this macro to Backspace.

You might get a warning about replacing the functionality of the current Backspace key, but you can just ignore that (because the script takes the normal functionality of Backspace into account).

[Extra] At any point in time, if you would like to restore the original functionality, an easy way is by deleting the macro (or restoring it to its default shortcut) and then:

    Shortcuts -> Editor -> Basic Key Mapping -> Delete left character


and change it back to Backspace.

1. Test it out! (Note that in the .gif, I have "Auto Complete Parenthesis" from Adv. Editor options turned on).

• Wow! A+ answer, thank you so much. This is exactly what I wanted. – AlkaKadri Dec 29 '17 at 17:22
• @AlkaKadri Cheers :) It's a problem I wanted to solve for a long time myself as well. – Troy Dec 29 '17 at 17:22

If the braces (or curly brackets) you want to delete simultaneously are delimiting the scope of a command, you can put the cursor on the word associated with the command and press Alt+Del.

For example,

\textbf{text}


will output

text

I'm not aware if there is a way to do the same for other types of bracket.

Source

• Alt+Del deletes the text within the brackets, not the brackets themselves. Also, even if did delete the brackets, I don't think that it's not the exact problem that the OP was trying to solve. – Troy Sep 25 '17 at 8:26
• If you look closely at section 2.15 of the source I provided, it says that: "the command is deleted including opening and closing braces", but it won't delete the text within the backets. – DecimalTurn Sep 26 '17 at 0:15
• Fair enough, but this still does not answer the question imo. The OP is asking about deleting the closing brace automatically, in the scenario when OP has deleted the opening brace. (like the python example) – Troy Sep 26 '17 at 0:30
• The way I understand the question, is that the OP wish there were a way to delete the closing bracket as he's deleting the opening one automatically (or simultaneously). However, I agree that it doesn't answer the case where the brackets aren't linked to a command which is probably what the OP was referring to. – DecimalTurn Sep 26 '17 at 1:01
• Even if the brackets were linked to a command, to use that Alt+Del shortcut to delete the brackets (and the command!) would require me to use the arrow keys to shift the cursor left, then enter the keyboard shortcut. At that point it seems to make more sense and typing efficiency to just press Backspace and Del to delete both brackets around the cursor.. – Troy Sep 26 '17 at 1:14