Sublime Text 3引用了自动对功能,效果很好。但是,出于某种原因,当后续字符是分号时,它会停止工作。所以,在这里输入单引号或双引号:
echo ^
将输入两个引号并将光标置于它们之间,同时在此处执行相同的操作:
echo ^;
将导致Sublime Text 3仅输入打开单引号或双引号。
我正在用PHP编程,有一个小故障,我最常输入分号,然后我回到那一行并编写实际代码。许多片段和宏也会以分号形式出现。因此,在这种情况下,ST3的这种行为有点烦人。
有没有解释,为什么自动配对在Sublime Text 3中被限制在分号之前不起作用?而且 - 最重要的是 - 有没有办法解决这个有问题的行为?
默认情况下,只有当以下字符是\t
(制表符),(空格),
)
,]
,}
,>
或行尾时,Sublime才会自动配对引号。幸运的是,可以通过基于默认键绑定创建自定义键绑定来轻松修改此规则。打开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中都有效。