默认情况下,Sublime 仅在以下字符为\t(制表符)、(空格)、)、]、}、>、或在行尾。幸运的是,可以通过基于默认键创建自定义键绑定轻松修改此规则。打开 Preferences -> Key Bindings-User 并添加以下内容(如果文件为空,则在开头用左方括号 [ 将所有内容括起来,在末尾用 ] 关闭 [ <content> ]):
// allow matched quotes before semi-colon
// double 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", "match_all": true }
]
},
// single 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.single", "match_all": true }
]
},
// curly brackets
// parentheses and square brackets already work
{ "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": "following_text", "operator": "regex_contains", "operand": "^(?:\t| |\\)|]|\\}|>|$|;)", "match_all": true },
如果您发现您想在另一个字符之前自动配对,只需在 "operand" 的分号后放置一个管道 | 并添加您想要的字符。
我应该注意,这在 Sublime Text 2 和 3 中都可以使用。