Is there a way to get a syntax type to define keyboard shortcuts, or to set a keyboard shortcut to depend on the syntax type (perhaps under the "context"
) setting?
My quoted lists '(1 2 3)
get entered in like this: '(1 2 3)'
because Sublime applies this helpful (but not in this case) behavior.
Here is the relevant bit of the Default (OSX).sublime-keymap
file
// Auto-pair single quotes
{ "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": "preceding_text", "operator": "not_regex_contains", "operand": "['a-zA-Z0-9_]$", "match_all": true },
{ "key": "eol_selector", "operator": "not_equal", "operand": "string.quoted.single", "match_all": true }
]
},
{ "keys": ["'"], "command": "insert_snippet", "args": {"contents": "'${0:$SELECTION}'"}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true }
]
},
{ "keys": ["'"], "command": "move", "args": {"by": "characters", "forward": true}, "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": "^'", "match_all": true }
]
},
I did this simple thing:
Put this:
// Singlequote for lisp
{ "keys": ["'"], "command": "insert_snippet", "args": {"contents": "'"}, "context":
[
{ "key": "selector", "operator": "equal", "operand": "source.lisp"}
]
},
in your user-keys-bindings.
Step it up a notch:
There's still one unusual case in this scenario:
'|'
(pipe represents caret), after pressing backspace, it would still delete both singlequotes.
To fix this, add this to your user-key-keybindings:
{ "keys": ["backspace"], "command": "left_delete", "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "'$", "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^'", "match_all": true },
{ "key": "selector", "operator": "equal", "operand": "source.lisp"}
]
},
IMPORTANT NOTE:
As stated by pickwick in comments, you should - of course - change
"source.lisp"
to"source.<lisp-of-your-choice>"
or rather to"source.<name-of-syntax-of-your-choice>"
or even rather-er:"source.<name-of-scope-the-syntax-you're-using-is-using>"
.
ScopeAlways is a plugin can show you the correct name of scope that your syntax is using. It's very likely going to be something like "source.lisp"
for CL, "source.clojure"
for clojure, maybe "source.scheme"
if you hang out with the cool kids, etc...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With