当用户输入时,我如何 运行 一个 sublime 插件命令;或者 }

How can I run a sublime plugin command when user types ; or }

我有一个 Sublime 插件,但我想在用户键入 };

时执行某些操作

我已经尝试在 on_modified 事件中处理它,但我无法让它正常工作。

这是最好的方法吗?

添加与您的插件捆绑在一起的新键绑定。例如,这里是触发大括号自动配对的默认键绑定:

{ "keys": ["{"], "command": "insert_snippet", "args": {"contents": "{[=10=]}"}, "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 }
    ]
},
{ "keys": ["{"], "command": "wrap_block", "args": {"begin": "{", "end": "}"}, "context":
    [
        { "key": "indented_block", "match_all": true },
        { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
        { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
        { "key": "following_text", "operator": "regex_match", "operand": "^$", "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 }
    ]
},

请注意,通过使用单个字符作为键绑定,您将拦截按键并且它不会显示在缓冲区中。如果要将字符发送到缓冲区,则需要将其作为命令的一部分放在那里。

您可以在示例中看到这一点。两个 insert_snippet 命令用花括号包裹任何参数:"contents": "{[=12=]}"wrap_block 命令发送花括号作为开始和结束参数:"args": {"begin": "{", "end": "}"}