Autocomplete 崇高文本2:试图摆脱美元符号
我试图在Sublime中定义一个键绑定,使其自动播放美元符号“$”,与自动播放以下符号的方式相同:Autocomplete 崇高文本2:试图摆脱美元符号,autocomplete,escaping,sublimetext2,key-bindings,dollar-sign,Autocomplete,Escaping,Sublimetext2,Key Bindings,Dollar Sign,我试图在Sublime中定义一个键绑定,使其自动播放美元符号“$”,与自动播放以下符号的方式相同: " ( [ { " 我打开了默认的keymap文件并添加了以下代码: // Auto-pair dollar signs { "keys": ["\$"], "command": "insert_snippet", "args": {"contents": "\$$0\$"}, "context": [ { "key": "setting.auto_match_enab
- "
- (
- [
- {
- "
// Auto-pair dollar signs
{ "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 }
]
},
{ "keys": ["\$"], "command": "insert_snippet", "args": {"contents": "\$${0:$SELECTION}\$"}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true }
]
},
{ "keys": ["\$"], "command": "move", "args": {"by": "characters", "forward": true}, "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": "^\$", "match_all": true }
]
},
{ "keys": ["backspace"], "command": "run_macro_file", "args": {"file": "Packages/Default/Delete Left Right.sublime-macro"}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "\$$", "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^\$", "match_all": true }
]
},
注意转义的美元符号\$。在编辑器中,它们以红色突出显示,当我试图保存文件时,会收到一条无效的转义错误消息。转义美元符号的正确方法是什么?注意转义
您将在这里经历两层编程。首先是Python,然后是JSON。因此,您的转义美元符号实际上需要转义两次:
"args": {"contents": "\\$$0\\$"}
升华文本读取设置文件后,Python将去掉第一个转义,留下以下JSON表示
"args": {"contents": "\$$0\$"}
然后,一旦JSON被解析,您将最终得到
"args": {"contents": "$$0$"}
不要逃避按键
您不需要在keys
列表中转义$
。击键是一个字面上的美元符号,因此不需要转义
以下是第一个设置的外观:
{ "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 }
]
},
上帝保佑你,我的朋友