Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 731
Ofek Agmon Avatar asked Oct 19 '25 14:10

Ofek Agmon


1 Answers

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.

like image 178
Frank Tan Avatar answered Oct 23 '25 08:10

Frank Tan