Sublime Forum

Tiny "bug" with automatically inserted characters

#1

When you insert a character like a quote ’ or a bracket , Sublime will helpfully insert the closing character.
This is great, but the implementation has one tiny problem:
If I insert a bracket , then delete it, I’m left with the automatically inserted closing bracket.

This hurts a lot when you’re trying to type without looking at the screen: suddenly, if you make a mistake and delete it, which I often do when coding, you’re left with lots of garbage characters filling your line.

The solution is just to delete the extra added character when deleting the original.

Edan
P.S. I’m pretty sure this behavior isn’t in a user package and is part of Sublime proper.

0 Likes

#2

Another small idea in the same spirit:

Sublime will automatically overwrite “special” characters for you. For example, if my cursor is next to a “(” and I try to insert a “(”, it will be overwritten.
This is for the same reason I wrote about earlier, to help people when Sublime automagically completes all sorts of characters.

The problem is, this happens all the time. Even if my cursor is next to a special character that wasn’t automagically written for me, it will be overwritten. This is very weird, and oftentimes annoying, behavior.

0 Likes

#3

I keep running into this and it is very annoying.

Am I the only one having this issue? Cause I see that no one else has responded. Is this even happening to anyone else?

0 Likes

#4

Yes, it’s a feature. Sometimes I think about switching it off, but on the whole I think it does help. It spares me some gymnastics in typing curly brackets and square brackets with my keyboard layout. I get tripped up by it a lot too, though.

An alternative is to define snippets for (), {}, ], etc. But this approach forces you to think in advance too.

Brackets should appear where you need them just by thinking about it, shouldn’t they? :stuck_out_tongue: Maybe Jon can implement this in a future release.

0 Likes

#5

No, the autocompleting is definitely a good thing.
The only difference should be that autocompleted chars should be deleted when the first one is deleted.

This just keeps getting in my way all the time…

0 Likes

#6

This should do the trick too (if we’re talking about the same thing):

	<binding key="backspace" command="sequence 'leftDeleteCharacters' 'rightDeleteCharacters'">
		<context name="allFollowingCharacter" value="}" />
		<context name="allPrecedingCharacter" value="{" />
	</binding>
	<binding key="backspace" command="sequence 'leftDeleteCharacters' 'rightDeleteCharacters'">
		<context name="allFollowingCharacter" value="]" />
		<context name="allPrecedingCharacter" value="" />
	</binding>
	<binding key="backspace" command="sequence 'leftDeleteCharacters' 'rightDeleteCharacters'">
		<context name="allFollowingCharacter" value=")" />
		<context name="allPrecedingCharacter" value="(" />
	</binding>
0 Likes

#7

Oh man that is so awesome! Thank you for posting that. That has been driving me INSANE forever. Should be part of the default keybindings IMO.

e: Don’t know why I never bothered trying to disable these glitches, as they’ve been driving me nuts for a while. I also added bindings like above for quotes. The last thing that was mentioned that I’d like to turn off is the eating of the key press when you try to insert 2 closing braces next to each other. How exactly do you go about unbinding something like that from the user keybindings? I’ve just commented out the bindings in the default bindings file but that’s just going to get overwritten come updates.

0 Likes

#8

@guillermooo: Awesome! Thanks so much. This has definitely been at the top of my “annoying” lists for a while.

@Anomareh: Yeah trying to insert two closing brace characters next to each other is annoying. The way it should work, imo, is that it only overrides a quote character if it was previously auto-inserted, and you haven’t moved to a different line. I have no idea how to bind that though.

If anyone is interested, here’s @guillermooo’s code, but also including a fix for quotes:

[code]


<binding key="backspace" command="sequence 'leftDeleteCharacters' 'rightDeleteCharacters'">
	<context name="allFollowingCharacter" value="]" />
	<context name="allPrecedingCharacter" value="" />
</binding>

<binding key="backspace" command="sequence 'leftDeleteCharacters' 'rightDeleteCharacters'">
	<context name="allFollowingCharacter" value=")" />
	<context name="allPrecedingCharacter" value="(" />
</binding>

<binding key="backspace" command="sequence 'leftDeleteCharacters' 'rightDeleteCharacters'">
	<context name="allFollowingCharacter" value="'" />
	<context name="allPrecedingCharacter" value="'" />
</binding>

<binding key="backspace" command="sequence 'leftDeleteCharacters' 'rightDeleteCharacters'">
	<context name="allFollowingCharacter" value="&quot;" />
	<context name="allPrecedingCharacter" value="&quot;" />
</binding>
<binding key="backspace" command="sequence 'leftDeleteCharacters' 'rightDeleteCharacters'">
	<context name="allFollowingCharacter" value="<" />
	<context name="allPrecedingCharacter" value=">" />
</binding>

[/code]

EDIT: Added a fix for html brackets (<) as well.

0 Likes

#9

This should work reasonably well for the second annoyance regarding brackets:

[code]import sublime, sublimeplugin
import re

FIXME: doesn’t work with multiple selections.

class InsertUnbalancedBracketCommand(sublimeplugin.TextCommand):
def run(self, view, args):
matchingPairs = { “]”:"", “)”:"(", “}”:"{" }
openChar = matchingPairs[args[0]]

    openCharsRegex = re.compile(openChar)
    closeCharsRegex = re.compile("\%s" % args[0])

    currLine = view.substr(view.line(view.sel()[0]))
    closeCharProbablyNeeded = (len(openCharsRegex.findall(currLine)) !=
                                len(closeCharsRegex.findall(currLine)))

    if closeCharProbablyNeeded:
        view.runCommand("insertAndDecodeCharacters %s" % args[0])
    else:
        view.runCommand("move characters 1")[/code]

<binding key="/(\}\)\]])/" command="insertUnbalancedBracket $1"> <context name="allFollowingText" value="\}\)\]]" /> </binding>

EDIT: This sample plugin has a bug that won’t let you insert closing brackets in certain conditions.

0 Likes

#10

That’s an interesting approach, I’ll have to look into it tomorrow morning.

I was actually thinking of the using the new api to define a new context, something only available in the latest beta.

0 Likes