insert 命令只是准确插入您提供的文本,就好像您自己键入一样。如果您想执行插入文本之类的操作并指定光标结束的位置,则需要insert_snippet。
默认键绑定有几个使用insert_snippet 绑定的键示例作为演示这一点的命令。例如:
// Auto-pair 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 - punctuation.definition.string.end", "match_all": true }
]
},
正如这里所指出的,insert_snippet 命令将contents 作为其参数之一,它指定要插入的 sn-p 的文本,并且该文本可以包含诸如 sn-p 字段之类的内容,就像一个sublime-snippet文件就可以了。特殊字段$0 指定了光标应该放置的位置。
此特定示例还包含 context 项,这些项准确定义了此绑定应在何种情况下处于活动状态。
正如您的问题中所述,每次您输入冒号时都会触发您的绑定,这使您无法只输入一个冒号。因此,如果您还没有这样做,您可能还想为您的密钥添加上下文。
顺便说一句,Sublime Text 4 的一个特点是它会在输入 CSS 属性时自动注入一个类似这个的 sn-p。