Sublime Text 2:试图逃脱美元符号

时间:2015-12-06 07:23:35

标签: autocomplete escaping sublimetext2 key-bindings dollar-sign

我正在尝试在Sublime中定义一个keybind,让它自动转换美元符号“$”,就像它自动发布以下符号一样:

  • [
  • {

我打开了默认的keymap文件并添加了以下代码:

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

注意逃脱的美元符号,\ $。在编辑器中,它们以红色突出显示,当我尝试保存文件时,出现无效的转义错误消息。逃避美元符号的正确方法是什么?

1 个答案:

答案 0 :(得分:3)

注意逃脱

你在这里经历两层编程。第一个Python,然后是JSON。因此,您逃脱的美元符号实际上需要两次转义:

"args": {"contents": "\\$$0\\$"}

Sublime Text读取设置文件后,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 }
    ]
},