Automatically trigger abbreviation when expanding a snippet

Asked by Jan-Christian

I would like to employ a mix of abbreviations (defined with iab) and snippets. It would be nice if when triggering a snippet, I could also trigger a check to expand a possible abbreviation at the trigger point. For example, if I have the abbreviation ,a for \alpha, I’d like to type frac<Tab>,a<Tab>2<Tab> to get \frac{\alpha}{2} in Latex. How could I achieve this?

I already tried

exec "inoremap <silent> <Tab> <C-]><C-R>=UltiSnips_ExpandSnippetOrJump()<cr>"

This expands the abbreviation, but the following snippet expansion seems to receive the line information prior to the abbreviations expansion, therefore in the above example, I end up with the wrong text selected and an incorrect cursor position after the second <Tab>.

Question information

Language:
English Edit question
Status:
Solved
For:
UltiSnips Edit question
Assignee:
No assignee Edit question
Solved by:
Jan-Christian
Solved:
Last query:
Last reply:
Revision history for this message
SirVer (sirver) said :
#1

Yes, this will not work out. Vim does not give any guarantees when it empties its key buffers and when it executes the VimScrips required. UltiSnips is finely tuned to work when you enter the keys interactively and will fail in all other circumstances - unfortunately this is as good as it gets with the Vim is designed.

Have you tried using ycm? It offers completions for ultisnips snippets by default and should get you reasonably close to what you want to achieve - as close at it gets most likely. https://github.com/Valloric/YouCompleteMe

Revision history for this message
Jan-Christian (jchtt) said :
#2

Thanks for the link, but it’s not exactly what I was looking for.

In the meantime, I managed to get ultisnips to behave more or less like I want it to by hacking the jump_to function to manually do an abbrev check on the last word before the cursor. The crucial parts I had to figure out for this were the maparg() function in VimScript, allowing the lookup of an abbrev, and the cursor_moved function in Ultisnips which helps to get around the issue of wrong selections described in my question.

Revision history for this message
SirVer (sirver) said :
#3

That sounds interesting. Mind pushing a branch or sending in a patch? If this does not hurt in other cases, maybe we can just ship it on by default?