Sublime Text 3 - 仅将快捷方式应用于特定文件类型

Sublime Text 3 - apply shortcut only to specific file types

我正在使用 Sublime Text 3,我安装了 JSFormat 来格式化我的 .js 文件并配置了这样的键绑定:

{ "keys": ["ctrl+shift+f"], "command": "js_format" }

现在,我也想格式化我的 .css.html 文件,所以我找到了这个快捷方式:

{ "keys": ["ctrl+shift+f"], "command": "reindent" , "args": { "single_line": false } }

我想对我的 .js 文件使用 js_format,对我的 .css.html 文件使用 reindent

是否可以为每个快捷方式指定文件类型?

更新

这显然在 Sublime Text 4 中不再有效。

更新

后来我发现这是 Sublime Text 3: how to bind a shortcut to a specific file extension?

的副本

原答案

添加一个context:

{
  "keys": ["ctrl+shift+f"],
  "command": "js_format",
  "context": [
    {
      "key": "selector",
      "operator": "equal",
      "operand": "source.js"
    }
  ]
}

重要的部分是将 operand 设置为 source.js。您可以将 js 替换为您想要的任何文件扩展名。您还可以使用逗号指定其他来源。例如,这将导致命令应用于所有 .html.css 文件:

{ "key": "selector", "operator": "equal", "operand": "source.html, source.css" }

参见unofficial documentation on key bindings