Sublime Forum

[SOLVED] Key binding depending on preceding text

#1

When using the LaTeX syntax, I would like to press " and then ST3 would turn that into

``$SELECTION$0''

if scope is text.tex.latex and the preceding character is not \ but I can’t figure out how. What I have now is

{
  "keys": ["\""],
  "command": "insert_snippet", "args": {"contents": "``$SELECTION$0''"},
  "context": [
    {"key": "selector", "operand": "text.tex.latex", "operator": "equal"}
  ]
}

I would think that adding

{"key": "preceding_text", "operator": "not_regex_match", "operand": "\\", "match_all": true },

under context would fix it but apparently it doesn’t. Any tips?

0 Likes

#2

You need to double escape the slashes (for json and for regex)
I would change it to

{"key": "preceding_text", "operator": "not_regex_contains", "operand": "\\\\$", "match_all": true },

Aside you may also want to have a look at my LaTeXSmartQuotes package, because it also handles such cases. In addition it disables that for verbatim and code environments.

3 Likes

#3

Big thanks for LaTeXSmartQuotes! Does everything I had in mind :slightly_smiling:

0 Likes

#4

Where, I wonder, are all of the possible keys for context defined? I’d like to be able to check the filename before applying a keybinding. Do I use “key”: “filename”?

0 Likes

#5

One place to investigate is in the default key bindings to see what they’re doing.

Apart from that, the Unofficial Documentation includes information on the structure of a context that includes a list of contexts as well.

One way to go about making a key binding specific to a filename would be to use the selector key, which matches the scope specific to a certain type of file (e.g. text.html for any HTML file).

The list of selectors is not exclusive, though. You can create your own custom EventListener and implement the on_query_context method to provide your own contexts.

1 Like

#6

I saw all of those, but they were not particularly comprehensive. I understand that most use cases call simply for the ‘selector’ key to be used. Maybe there’s a plugin that shows me all the current settings?

0 Likes