Sublime Forum

Is it possible to not autocomplete single quotes only in text files? *solved*

#1

I’m completely OK with the autocompletion of brackets and double quotes everywhere, so I want to keep that as is, without any modification. I mean, the option "auto_match_enabled": true is exactly what I want.

I’d like to have just one exception: to not autocomplete single quotes in plain text files. (And to autocomplete them in any other files).

According to these pages of the documentation:
https://www.sublimetext.com/docs/key_bindings.html
https://www.sublimetext.com/docs/selectors.html
something like the following should accomplish that (the first “selector” key):

{ "keys": ["'"], "command": "insert_snippet", "args": {"contents": "'$0'"}, "context":
	[
		{ "key": "selector", "operator": "not_equal", "operand": "text", "match_all": true }, // should ignore plain text?
		{ "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.single - punctuation.definition.string.end", "match_all": true }
	]
},

However, it does not ignore plain text files.
What am I doing wrong?

0 Likes

#2

The default binding - without selector - is still active and applies due to more generic context.

You need to disable it by adding { "keys": ["'"], "command": "" }, in front of your custom binding.

2 Likes

#3

Aha!
From the official documentation about key bindings, I had no idea that there are some priorities of contexts. I was thinking that user’s key bindings just silently override the default ones.
I’d suggest to explicitly mention that fact in the documentation, with explicit examples. The following key bindings may be used as examples, with additional comments of why the bindings are specified in this exact order and what each binding (and each “key” within this binding) does.

{ "keys": ["'"], "command": "" },
{ "keys": ["'"], "command": "insert_snippet", "args": {"contents": "'$0'"}, "context":
	[
		{ "key": "selector", "operator": "not_equal", "operand": "text", "match_all": true },
		{ "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.single - punctuation.definition.string.end", "match_all": true }
	]
},
{ "keys": ["'"], "command": "insert_snippet", "args": {"contents": "'${0:$SELECTION}'"}, "context":
	[
		{ "key": "selector", "operator": "not_equal", "operand": "text", "match_all": true },
		{ "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.single - punctuation.definition.string.end", "match_all": true },
	]
},

By the way, I was about to add that I still don’t understand the purpose of the last binding here, the one with:

"keys": ["'"], "command": "move", "args": {"by": "characters", "forward": true}

but right at the moment of typing this, I realized this exact key binding deals with situations when you have the auto-completed ‘’ (with the caret positioned before the closing single quote) and you automatically type the closing single quote. In this case, this exact key binding does not allow to add another single quote before the existing one.
I believe, it makes perfect sense to explain such things in the official documentation.

0 Likes

#4

One more word about the official documentation.
I noticed the following key:

"key": "overlay_name"

in the default key bindings, while “overlay_name” is not mentioned in the documentation.
While it would be very good to have it in the documentation, together with possible values of it.
(The “overlay_has_focus” is not mentioned in the documentation as well).

0 Likes