# UltiSnips: how to avoid expanding twice in a row the same snippet?

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?

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

• It might be worth parameterizing the context so that \\theta is not hard-coded or specific to that single snippet. 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! Oct 11 '21 at 20:06

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).