Ofek Agmon
Ofek Agmon

Reputation: 5198

Sublime Text 3 - apply shortcut only to specific file types

I am using Sublime Text 3, and I installed JSFormat to format my .js files and configured the key binding like this:

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

Now, I also want to be able to format my .css and .html files, so I found this shortcut:

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

I want to use js_format for my .js files and use reindent for my .css and .html files.

Is it possible to specify a file type per shortcut?

Upvotes: 4

Views: 1042

Answers (1)

Frank Tan
Frank Tan

Reputation: 4412

Update

This apparently no longer works in Sublime Text 4.

Update

I've since discovered that this is a duplicate of Sublime Text 3: how to bind a shortcut to a specific file extension?

Original Answer

Add a context:

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

The important part is setting operand to source.js. You can replace js with whatever file extension you want. You can also specify additional sources with commas. For example, this would cause a command to apply to all .html and .css files:

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

See the unofficial documentation on key bindings.

Upvotes: 9

Related Questions