r-stein explained how you could define a keybinding in terms of a context specific to the document class (e.g. beamer or article). How could you do the same for a snippet?


\alert{$1} $0
    <!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
    <!-- Optional: Set a scope to limit where the snippet will trigger -->

You can't write arbitrary contexts for snippets, but only scopes. Therefore you can't create a snippet for that. However you can emulate the snippet behavior by creating a keybinding for the tab key, which is only trigger if the text before the caret is test. This keybinding should remove the word before it and insert the snippet. If you install the "Chain Of Command" package you can just use this keybinding:

    "keys": ["tab"],
    "command": "chain",
    "args": {"commands": [
        ["delete_word", {"forward": false}],
        ["insert_snippet", {"contents": "\\alert{$1} "}],
        { "key": "selector", "operand": "text.tex.latex" },
        { "key": "preceding_text", "operator": "regex_contains", "operand": "\\btest$", "match_all": true },
        { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
        { "key": "latextools.documentclass", "operand": "article" },

You can adapt it by changing the regex "operand": "\\btest$", to "operand": "\\byour_trigger$",

  • That's perfect. Sorry for the duplication, saw your answer to my comment on the other question as well. – Pravin Jul 4 '17 at 22:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.