Sublime Forum

[solved] Regex scope

#1

Is it possible to bind a key based on regex?

I need to bind to a macro in case I have a cursor \uv{somewhere_in_here}. There’s no specific language scope inside :frowning:

0 Likes

#2

You can use the preceding_text and following_text context operands. You would likely use it in combination with regex_match context operator.

See docs.sublimetext.info/en/latest/ … -a-context for more information on context

0 Likes

#3

Thanks, that works!

Code for future readers:

  // tab out of \uv{}
  { "keys": "tab"],
    "command": "run_macro_file", "args": {"file": "Packages/User/Macros/moveOutOfBracket.sublime-macro"},
    "context": 
      { "key": "selector", "operator": "equal", "operand": "text.tex" },
      { "key": "preceding_text", "operator": "regex_contains", "operand": "uv\\{.*", "match_all": true },
      { "key": "following_text", "operator": "regex_match", "operand": ".*\\}", "match_all": true }
    ]
  },
  // tab out of \uv{}
  { "keys": "tab"],
    "command": "move", "args": {"by": "characters", "forward": true},
    "context": 
      { "key": "selector", "operator": "equal", "operand": "text.tex" },
      { "key": "preceding_text", "operator": "regex_contains", "operand": "uv\\{.*", "match_all": true },
      { "key": "following_text", "operator": "regex_match", "operand": "\\}", "match_all": true }
    ]
  },
0 Likes