3

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?

3
  • 1
    Welcome to TeX.SX!
    – Bobyandbob
    Aug 4, 2017 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, 2017 at 19:16
  • @tmastny See my answer.
    – Troy
    Dec 29, 2017 at 15:12

2 Answers 2

9

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

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

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

7
  • 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, 2017 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. Sep 26, 2017 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, 2017 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. Sep 26, 2017 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, 2017 at 1:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .