Sublime Forum

Auto Pairing Quotes and Brackets before Comma/Colon

#1

Is it possible to make quotes and brackets do auto-pairing when the cursor is before a comma or colon?

The current behaviour is that quotes and brackets won’t auto-pair when the cursor is before them. For example, if I have a piece of code:

def function(arg1=, arg2):
                  ^

where the cursor is at the position pointed by “^”. If I type " now, the result will be:

def function(arg1=", arg2):
                   ^

However, I would like the quote to be auto-paired, like this:

def function(arg1="", arg2):
                   ^

Same thing for colon. For example, in python, if I have a piece of code like this:

   v
if :
    pass

which is the code completion if I typed if, and the cursor will be placed before the colon. Now if I would like to compare a variable with a string literal, I will need to type " for the string literal, but the quote will not be auto-paired, resulting something like this:
%E5%9B%BE%E7%89%87
which is quite annoying because sublime text will indicate that this line is invalid by displaying the rest of line in red.

0 Likes

#2

It’s defined by a default keybinding:

	// 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 }
		]
	},

obviously you would be interested in following_text.

1 Like

#3

Somewhat related: https://github.com/sublimehq/sublime_text/issues/3829

0 Likes

#4

Thanks for the solution. I didn’t notice that it’s defined as a keybinding before.

0 Likes