I was trying to use the same shortcut for a global snippet in my packages\user folder as I’m using for a syntax-specific snippet. This doesn’t work as the shortcut only triggers the last-listed snippet for the same shortcut in the Default (Windows).sublime-keymap file, regardless of the scope defined in the snippet not matching the current scope. So instead, I was wondering if maybe I could make the global snippet syntax-sensitive so that the shortcut would apply different escape characters to selected strings based on the current syntax, with a default escape character for syntaxes that don’t have a specific escape character defined. If that would be possible, how would I do it?
Syntax sensitive global snippets?
Snippets can only have one scope
assigned to them, and it’s used to determine when the snippet should appear in the AC panel and when it should appear in the command palette to make it situationally aware.
Key bindings just trigger commands, and are always active unless they have a context
that tells them that they don’t apply in certain situations.
What you want here sounds like a key binding that triggers insert_snippet
but which has a context that constrains the scope. For example:
{ "keys": ["ctrl+shift+e"], "command": "insert_snippet", "args": {
"contents": "This is a JavaScript file"
}, "context": [
{ "key": "selector", "operator": "equal", "operand": "source.js" },
]
},
{ "keys": ["ctrl+shift+e"], "command": "insert_snippet", "args": {
"contents": "This is a Python file"
}, "context": [
{ "key": "selector", "operator": "equal", "operand": "source.python" },
]
},
These two key bindings are on the same key, but do different things and are mutually exclusive; the context on each makes them only apply in the correct situation.
As you’ve noticed, key bindings are checked “bottom up” in the file; so, if you want to have a key that has context in it plus one for all other situations, the one that is global needs to be closer to the top of the file, so that it is found and used only after the ones with context have been skipped over.
Ah yes. That’s the sort of thing I was looking for, but didn’t know where to begin with setting it up. Thanks!