Sublime Forum

Unexpected consequences of "auto_match_enabled": false,

#1

I’ve got a bit stuck. I’ve looked at this post

and this is what i’ve done

{ "keys": ["shift+alt+9"], "command":"multicommand", "args":{ "commands":[ {"command":"insert", "args": {"characters": "()"}}, {"command": "move", "args": {"by": "characters", "forward": false}}, ] } },

So I turned off auto_match_enabled, because having to delete the spare bracket when I don’t want it is time consuming. All be it a small amount of time but over 100,000s of brackets it adds up.

So the above key binding is so that when I press alt+( i get (cursor). Though this

{ "keys": ")"], "command": "insert_snippet", "args": {"contents": "($0)"}, looks better.

However, to my problem turning auto_match off has disabled select some text and then wrap it in quotes, brackets of your choice, which is very useful.

How do I turn auto_match off, but keep the wrap text in brackets functionality, or what’s the work around please ?

Lozminda

0 Likes

#2

You could also select text and type ( to wrap it into parentheses. From that point of view there’s no difference between braces, brackets, parentheses and quotation marks with regards to auto_match_enabled. All of them behave the same way.

That said, the auto_match feature is purely driven by key bindings. You can simply search for auto_match_enabled in the Default.sublime-keymap file and create some overrides with that setting commented out.

Exmple for double quotes

	// Auto-pair quotes
	{ "keys": ["\""], "command": "insert_snippet", "args": {"contents": "\"$0\""}, "context":
		[
			// { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
			{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
			{ "key": "following_text", "operator": "regex_contains", "operand": "^(?:\t| |\\)|]|\\}|>|$)", "match_all": true },
			{ "key": "preceding_text", "operator": "not_regex_contains", "operand": "[\"a-zA-Z0-9_]$", "match_all": true },
			{ "key": "eol_selector", "operator": "not_equal", "operand": "string.quoted.double - punctuation.definition.string.end", "match_all": true }
		]
	},
	{ "keys": ["\""], "command": "insert_snippet", "args": {"contents": "\"${0:$SELECTION}\""}, "context":
		[
			// { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
			{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true }
		]
	},
	{ "keys": ["\""], "command": "move", "args": {"by": "characters", "forward": true}, "context":
		[
			{//  "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
			{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
			{ "key": "following_text", "operator": "regex_contains", "operand": "^\"", "match_all": true },
			{ "key": "selector", "operator": "not_equal", "operand": "punctuation.definition.string.begin", "match_all": true },
			{ "key": "eol_selector", "operator": "not_equal", "operand": "string.quoted.double - punctuation.definition.string.end", "match_all": true },
		]
	},
	{ "keys": ["backspace"], "command": "run_macro_file", "args": {"file": "res://Packages/Default/Delete Left Right.sublime-macro"}, "context":
		[
			// { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
			{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
			{ "key": "preceding_text", "operator": "regex_contains", "operand": "\"$", "match_all": true },
			{ "key": "following_text", "operator": "regex_contains", "operand": "^\"", "match_all": true },
			{ "key": "selector", "operator": "not_equal", "operand": "punctuation.definition.string.begin", "match_all": true },
			{ "key": "eol_selector", "operator": "not_equal", "operand": "string.quoted.double - punctuation.definition.string.end", "match_all": true },
		]
	},
0 Likes

#3

Thanks for your reply, I’ll get into it later today.

I did have a thought about a reverse alternative, leaving auto_match_enabled true, that deleted the last bracket, but I’ve just had a go and it doesn’t work…phoo

Cheers

0 Likes

#4

Ps I’m guessing the regex that ST3 uses is the python regex, it seems to contain syntax I haven’t come across before (have just started looking at regex). Here’s the link I used to get going…

0 Likes

#5

What do you mean with “deleted the last bracket” ? There is a keybiding for ) which triggers a caret movement instead of inserting the ) in order to avoid duplications. Is that what you don’t like?

That’s the default binding.

	{ "keys": [")"], "command": "move", "args": {"by": "characters", "forward": true}, "context":
		[
			{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
			{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
			{ "key": "following_text", "operator": "regex_contains", "operand": "^\\)", "match_all": true }
		]
	},

You may want to add { "keys": [")"], "command": ""} to your key bindings to override it then.

0 Likes

#6

@deathaxe, thanks for all your input. I’ve done one of several things:
a) Expressed myself unclearly
b) Overcomplicated things
c) Possibly some other stuff.

The solution I found was to leave auto match_enabled, so that I get all the behaviour I want from that functionality and then define

{ "keys": ["alt+shift+9"], "command": "insert", "args": {"characters": "("}},

in my keybindings, so if I want an open bracket (without it being closed automatically) I press “alt+(” and I get an open bracket and nothing else.

It’s a bit of a faff having to redefine all the open brackets, but hey.

In the code you’ve posted what does key mean (rather than keys)? I’m guessing it is something to do with context? It’ll all come in handy for other functionality if I don’t use it this time.

Thanks again

Ps Here are all my keybindings in case it’s useful for someone else:

{ "keys": ["alt+shift+9"], "command": "insert", "args": {"characters": "("}},
{ "keys": ["alt+shift+2"], "command": "insert", "args": {"characters": "\""}},
{ "keys": ["alt+shift+["], "command": "insert", "args": {"characters": "{"}},
{ "keys": ["alt+["], "command": "insert", "args": {"characters": "["}},
{ "keys": ["alt+'"], "command": "insert", "args": {"characters": "'"}},
0 Likes