In my Sublime Text 3, I use JsFormat to format my javascript source code. Its key binding follows:
1 2 3 4 |
{ "keys": ["ctrl+alt+i"], "command": "js_format", "context": [{"key": "selector", "operator": "equal", "operand": "source.js,source.json,source.html"}] } |
I have noticed that the JsFormat used the selector context operand to make sure that the js_format command would only run on file types “.js”, “.json” or “.html”. This is to avoid key clashes with other commands such as the SublimeAStyleFormatter which is used to format my C family source code.
Now I want to add another command that uses a python script called tidy_xml.py to format my XML source code. At the same time, I want to use the same key binding, i.e. the “ctrl+alt+i”, to run the formatting command. Actually, the “ctrl+alt+i” is my universal formatting shortcut. So I set the following binding which followed the idea from JsFormat:
1 2 3 4 |
{ "keys": ["ctrl+alt+i"], "command": "tidy_xml", "context": [{"key": "selector", "operator": "equal", "operand": "source.xml"}] } |
Unfortunately, this doesn’t work. But why did the JsFormat one work? Believe me, I asked myself a lot about this and I also googled a lot. Finally, I found that the scope for XML is not “source.xml”, but “text.xml”. WTF! So this works:
1 2 3 4 |
{ "keys": ["ctrl+alt+i"], "command": "tidy_xml", "context": [{"key": "selector", "operator": "equal", "operand": "text.xml"}] } |
Here are the steps I used to find out the scope of XML type (other types also apply):
- Right click your “Applications/Sublime Text.app”, and select the “Show Package Contents”.
- Copy and paste the file “Contents/MacOS/Packages/XML.sublime-package” to somewhere else, says Downloads.
- Rename XML.sublime-package to XML.zip, and double click it to unzip
- Open XML-Processing-Instruction.sublime-snippet in the unzipped folder XML, and then you will find the scope value
Hope this work out for you too If anyone of you managed to make the regex_match work so that we don’t have to manually check the scope for each file type in the future, please let me know.
Awesome, exactly what I needed, I was trying source.xml too.
For reference, https://gist.github.com/iambibhas/4705378 has a list of Sublime Text 2’s scopes. I imagine they will be the same as Sublime Text 3.
Hi,
So how to create a key binding that have not set in Default-Keybinding.
ex, I would like to create a key binding for Project->Close Project
but I don’t know how to create. I use this code in user-key binding but don’t know which should put in the command for Project->Close Project
{ “keys”: [“ctrl+c,p”], “command”: “?” }
I tried to put “close_project” as the command, but it didn’t work. I don’t think Sublime has a built-in support for this, so you may have to write your own script somehow to implement this command feature.