0

I have this snippet for latex:

# theta --> \theta
context "math()"
snippet theta "theta" iA
\theta
endsnippet

My problem is that if I type theta, it is transformed automatically to \theta, but then if I press <tab>, it expands to \\theta which I do not want.

One solution could be to expand only automatically, and not when you press my UltiSnipsExpandTrigger key (e.g. <tab>). I have read :h UltiSnips-snippet-options and did not find a solution. I want to keep the i in-word expansion.

What can I do?

Related: Ultisnips: autocompletion of (). Make snippet not expand again

1
0

This context solves the issue:

global !p
def banned_before():
    x = vim.current.window.buffer
    y = vim.current.window.cursor[0]
    z = vim.current.window.cursor[1]
    banned_before = ('\\theta',)
    bufferslice = x[y-1][:z]  # contains the text before cursor of the current line
    try:
        return (not bufferslice.endswith(banned_before))
    except:
        return True
endglobal
# theta --> \theta
context "banned_before()"
# use `context "math() and banned_before()"` for both
snippet theta "theta" iA
\theta
endsnippet
2
  • It might be worth parameterizing the context so that \\theta is not hard-coded or specific to that single snippet.
    – D. Ben Knoble
    Oct 11 '21 at 18:48
  • Oh, I did not think of it, that's much better than what I did (keep a list of greek letters in my context function). Thanks a lot!
    – Sylve
    Oct 11 '21 at 20:06
0

Alternatively, you can use a negative lookbehind regex, as in

context "math()"
snippet "(?<!\\)theta" "\theta" riA
\theta
endsnippet

The r in riA tells Ultisnips that the snippet trigger is a python regular expression, and (?<!\\) says that we do not wish to match \ before theta (we have to escape the backslash).

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.