+11

Key binding context to only react on e.g. Python code

Oktay Acikalin 7 years ago • updated by Pat 6 years ago 10
What do I have to put into the context list to only have my key binding react on scope source.python?
I want to bind the same key to somewhat similar scripts but for different languages (e.g. Python, PHP, XML, Bash).
+1
It's the "selector" argument, in ST2 simply put this line in your context block:

{ "key": "selector", "operator": "equal", "operand": "source.python" }
Like this?

{ "keys": ["ctrl+alt+shift+t"], "command": "python_tidy", "context":
[
{ "key": "selector", "operator": "equal", "operand": "source.python" }
]
},
Yes, exactly.
This an example from a file I currently use:
[
{ "keys": ["alt+o"], "command": "switch_file", "args": {"extensions": ["pas", "dfm"]}, "context":
[
{ "key": "selector", "operator": "equal", "operand": "source.pascal" }
]
},
{ "keys": ["ctrl+shift+r"], "command": "delphi_switch_head_body", "context":
[
{ "key": "selector", "operator": "equal", "operand": "source.pascal" }
]
}
]
+1
For me it doesn't work :(. It seems like the context has no effect.
It work for me at least on Build 2025 on Windows 7 64.
Do you mean that the keymap is always called whatever the current file edited ?

Are you sure the keymap file is loaded ?
Are you sure you don't redefined the keymap somewhere else ? (try to call somtehing else)
+4
Ok. Let's see. I have 2025 on OSX 10.6.6.

In my user defined keybindings I have:

{ "keys": ["ctrl+alt+shift+t"], "command": "python_tidy", "context":
[
{ "key": "selector", "operator": "equal", "operand": "source.python" }
]
},
{ "keys": ["ctrl+alt+shift+t"], "command": "perl_tidy", "context":
[
{ "key": "selector", "operator": "equal", "operand": "source.perl" }
]
},

These are not bound anywhere else.
When hitting save I can see that sublime reloads my two keymap files.

So here's what I discovered so far:
1. After starting sublime, I can go into any file and hit the key combo to invoke python_tidy - regardless of the file type/ext/syntax.
2. I add a prefix to the commands like "xxx" so that they won't work anymore and hit save.
3. When hitting the combo I get a strange letter in the view. Sounds deactivated. The console throws a proper error message.
4. I remove the prefix from the commands and hit save.
5. When hitting the combo they react as intended and not in every file (e.g. python-file -> python_tidy, perl-file -> perl_tidy, anywhere else -> strange letter).
Well, I think this look like a bug.
Only Jon or someone with OSX could help you.

Good luck.
+4
So my solution for disabling a key binding when no context should match is another entry:

{ "keys": ["ctrl+alt+shift+t"], "command": "", "context":
[
{ "key": "selector", "operator": "not_equal", "operand": "source.python" },
{ "key": "selector", "operator": "not_equal", "operand": "source.perl" }
]
},

This effectively disables the binding when not in python or perl code.

Jon, should I open a bug entry for this?
+2

I am facing the same bug. This keymap is active on all types of files, so the context is not working.


{ "keys": ["super+shift+x"], "command": "something_cool", "context" : [ { "key": "selector", "operator": "equal", "operand": "source.ruby", "match_all": true } ]}

I'm experiencing the same issue (ST 2.0.1 build 2217 on 32-bit Win7). I created a keybinding for auto-doubling percent signs (%) in AutoHotkey and dos batch scripts. However, it shows up in most/all files. Here's my code:

// Auto-pair brackets
{ "keys": ["%"], "command": "insert_snippet", "args": {"contents": "%$0%"}, "context":
	[
		{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
		{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
		{ "key": "following_text", "operator": "regex_contains", "operand": "^(?:\t| |\\)|]|;|\\}|$)", "match_all": true },
		{ "key": "selector", "operator": "equal", "operand": [
			{"key": "source.dosbatch"}, {"key": "source.ahk"} 
		]}
	]
},