Sublime Forum

Snippet Triggering WITHOUT <TAB> or similar

#1

From what I gather, the current state of Sublime Snippets does not support automatically triggered snippets.

For example, suppose I want to type &=, but minimizing the amount of keystrokes and hand movement. A potential feature would be the ability to type == and have it automatically transform to the desired text.

A more similar example would be wanting begin to always expand to \begin{$0} Content \end{$0} when begin is placed at the beginning of the line.

A great example of this behavior in action can be found here, which showcases its usefulness with rapid LaTeX writing.

This feature already exists in Vim/NVim with the UltiSnips package, and it can easily be done with Emacs Yasnippets. It’d be great if this feature existed with Sublime Snippets, as it speeds up typing tremendously.

0 Likes

#2

This seems solidly in the realm of things that can easily be provided by packages such as the ones that you mention; is there some compelling reason not to install something to give you the feature that you want?

In any case, you can also achieve this without packages by using key bindings, if you want certain key sequences to take different actions in different situations (which seems like a better fit).

It’s probably a stick ysituation (plugin or not) to distinguish between == meaning that you meant to insert &= versus meaning that you actually meant to type == though (if that’s a thing).

    { "keys": ["=", "="], "command": "insert",
       "args": {"characters": "&="},
       "context": [
           { "key": "selector", "operator": "equal", "operand": "text.tex.latex" },
       ],
    },

    { "keys": ["b", "e", "g", "i", "n"], "command": "insert_snippet",
       "args": {
            "contents": "\\begin{$1} ${0:Content} \\end{$1}"
       },
       "context": [
           { "key": "selector", "operator": "equal", "operand": "text.tex.latex" },
           { "key": "preceding_text", "operator": "regex_match", "operand": "^b?e?g?i?n?" },
       ],
    },

Examples like expanding a couple of characters in rapid succession (like == to &=) are better suited to this; something that requires more state than just the key pressed (like recognizing begin only at the start of a line) are probably better suited to a key binding that works in combination with a custom context though (which would require a plugin).

1 Like