Sublime Forum

Regex not working

#1
    { "keys": ["ctrl+space"], "command": "expand_selection", "args": {"to": "line_prev"}, "context": [
        { "key": "preceding_text", "operator": "regex_contains", "operand": "[^ \t\n][ \t]*\n[ \t]*" },
    ] },

Why doesn’t this key binding trigger when placed here? It eliminates tabs/spaces to the left, then a newline, then more tabs/spaces, then a non-space character. It 100% should be triggering, but it’s broken.

    //
  {CURSOR}  //
    //
0 Likes

#2

The context keys preceeding_text and following_text apply only to text on the same line as the caret; preceeding_text is the text from the start of the line up to the cursor, and similarly following_text applies to all of the text from the point after the cursor to the end of the line.

Thus, since your regex contains a \n and that can’t possibly appear prior to the cursor, the regex never matches.

An example of a context that would match there (which is likely not what you actually want, but just for illustrative purposes) is:

{ "key": "preceding_text", 
  "operator": "regex_contains", 
  "operand": "^\\s{1,}$" },

Which says that the text prior to the cursor must be 1 or more whitespace characters; so as long as the cursor is not in the first column and everything to the left of it is whitespace, the context will resolve to True.

2 Likes