[Tex/LaTex] Automatically Delete Corresponding Bracket in TeXstudio

auto-completionbracketstexstudio

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?

Best Answer

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.

pic1

  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.

pic2

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).

gif

Related Question