文件相关的键绑定

File Dependent Key Bindings

我在我的 Sublime Text 3 的 Preferences > Key Bindings - User 配置文件中添加了以下代码:

{ "keys": ["ctrl+b"], "command": "insert_snippet", "args": {"contents": "<strong>${0:$SELECTION}</strong>"} },

因此,当用户在编辑器中按下 Ctrl+B 时,当前选择被 HTML 标签包围 <strong><strong>.

无论如何,我可以让这个设置文件类型依赖吗? IE。如果用户正在 *.txt*.md 文件中工作,则在编辑器中按 Ctrl+B 应该用 Markdown 包围选择粗体标记 (**),并且在编辑任何其他类型的文件时(一般或特别是 *.html 文件),然后用 HTML 标记包围,如上例所示。

这在 Sublime Text 3 中可行吗?

这可以通过 context 参数来完成:

"context": [
    {
        "key": "selector",
        "operator": "equal",
        "operand": "source.php"
    }
]

selector Returns the name of the current scope.

operator Type of test to perform against key‘s value. Defaults to equal.

operand The result returned by key is tested against this value.

More help see: http://docs.sublimetext.info/en/latest/reference/key_bindings.html

例子

仅在HTML中识别Ctrl+B

// bold snippet for html
{ 
    "keys": ["ctrl+b"], 
    "command": "insert_snippet", 
    "args": {"contents": "<strong>${0:$SELECTION}</strong>"},
    "context": [
        {"key": "selector", "operator": "equal", "operand": "text.html.basic"}
    ]
},

用于识别 Markdown 和纯文本中的 Ctrl+B

// bold snippet for markdown and plain text
{ 
    "keys": ["ctrl+b"],
    "command": "insert_snippet", 
    "args": {"contents": "**${0:$SELECTION}**"},
    "context": [
        {"key": "selector", "operator": "equal", "operand": "(text.html.markdown, text.plain)"}
    ]
},