Using UltiSnips, I'm wondering if it's possible to do some lookahead assertion when detecting the trigger.

For example, this works:

snippet "dv" "derivative" ir

But this doesn't even get triggered:

snippet "dv(?=[ \n])" "derivative" ir

I've had similar issue with negative lookahead, except it always gets triggered regardless. I'd like to know if there is some possible workaround.

1 Answer 1


Lookaheads are currently not supported by UltiSnips*, these include both positive and negative lookahead assertions.

You could use a context (see :h UltiSnips-custom-context-snippets) to inspect the text after the cursor. The cursor position is accessible with snip.cursor:

global !p

def text_after_cursor_matches(snip, string):
    (line, col) = snip.cursor
    text_after_cursor = snip.buffer[line][col:]
    # print(text_after_cursor)
    return re.match(string, text_after_cursor)


context text_after_cursor_matches(snip, "[ \n]")
snippet dv "derivative" i

Above I used a custom function text_after_cursor_matches() to check if the characters that follow after the cursor matches a certain regex ("[ \n]" in this case). If True, the snippet is expanded.

*: as implied from the maintainer's response to this issue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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