Sublime Forum

Auto completing brackets within quotes

#1

Hey guys.

At the moment when I open a parenthesis in newly opened quote marks, the parenthesis doesn’t close automatically, is there a way to enable this behaviour in sublime text 2?

So instead of showing “(” it goes straight to “()”

Cheers…

0 Likes

#2

Have a look at the following snippet in Preferences > Key Bindings – Default:

[code] // Auto-pair brackets
{ “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 }
	]
},[/code]

You can change the operand of “regex_contains” to include a quote.

0 Likes

#3

[quote=“nick.”]Have a look at the following snippet in Preferences > Key Bindings – Default:

[code] // Auto-pair brackets
{ “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 }
	]
},[/code]

You can change the operand of “regex_contains” to include a quote.[/quote]

ahh yes… I’m not great with regular expressions, could you give me an idea as to how I could edit it?

0 Likes

#4

It is a bit trickier since we’re in JSON; you have to escape the quote (because the whole string is in double quotes), and then you have to escape the escape. Thus:

"regex_contains", "operand": "^(?:\t| |\\)|]|;|\\}|\\\"|$)"

Full key binding:

[code]// Auto-pair brackets
{ “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 }
	]
},[/code]

You may want to put that in your Key Bindings – User so you don’t lose the setting on update.

0 Likes

#5

This is awesome. Thanks so much :smile:

0 Likes