Sublime Forum

Regex in user key bindings

#1

Iā€™m trying to get a regex to work with my key binding. Specifically, Iā€™m trying to match any block comments. Iā€™ve gotten the following to work matching the full scope, but I canā€™t seem to get it to work using regex_contains so that I can just match ā€œcomment.blockā€. Help?

, { "keys" : "enter"] , "command" : "insert_snippet" , "args" : {"contents": "\n* "} , "context" : { "key" : "selector", "operator": "equal", "operand": "source.js comment.block.documentation.js" } ] }

0 Likes

#2

I know nothing about this so I apologise for mis-information in advance. But I would take a guess that if there is an operator ā€œequalā€ then there might also be an operator ā€œcontainsā€ - even though I canā€™t see one in the default key-bindings. Worth a stab, perhaps :smile:

You should also post your regex versionā€¦

The asterisk might also need escaping *, but youā€™ve stated that it worksā€¦

0 Likes

#3

Yeah, I donā€™t see a contains (and the parser chokes on it anyway). There is a regex_contains, which Iā€™m trying to use as such:

{ "key" : "selector", "operator": "regex_contains", "operand": ".*comment.block.*" }

Iā€™ve tried variations of that with or without the .*, using ^ and $, etcā€¦ Canā€™t seem to get anything to work. I canā€™t even get not_regex_contains to work (which should match everything!)

0 Likes

#4
    { "key" : "selector", "operator": "regex_contains", "operand": "comment\.block" }
0 Likes

#5

regex_contains only works with the preceding_text, text, and following_text keys.

To match vs all block comments, you just want to use a selector:

    { "key" : "selector", "operator": "equal", "operand": "comment.block" }

You can test selectors via positioning the caret at the desired location, and then using the console:

view.match_selector(view.sel()[0].a, "comment.block")
0 Likes

#6

Thanks jps :smile:

0 Likes

#7

[quote=ā€œjpsā€]regex_contains only works with the preceding_text, text, and following_text keys.

To match vs all block comments, you just want to use a selector:

    { "key" : "selector", "operator": "equal", "operand": "comment.block" }

You can test selectors via positioning the caret at the desired location, and then using the console:

view.match_selector(view.sel()[0].a, "comment.block") [/quote]

Very helpful, thanks a lot.

0 Likes

#8

I was looking to do this also. Does this mean that there is no way to set a key binding for more than one scope. For example I have the scope source.python and source.python.django. I tried to use the regex_contains to set a key binding for every python scope but couldnā€™t get it to work.

thanks.

0 Likes