0

I have vim with UltiSnips and it's working but only if I write the whole trigger. Is there a way to expand the snippet writing only a part of the trigger? I used to work with SnipMate and that works.

e.g.: If I have a trigger like follows:

snippet someTrigger
blah blah
endsnippet

It expand the snippet when I type:

someTrigger<tab>

But it doesn't when I type

some<tab>

3 Answers3

2

I will offer two examples right of the documentation of ultisnips (I am familiar with these because I wrote these particular examples myself).

First one, doesn't depend on any external plugins:

function! ExpandPossibleShorterSnippet()
  if len(UltiSnips#SnippetsInCurrentScope()) == 1 "only one candidate...
    let curr_key = keys(UltiSnips#SnippetsInCurrentScope())[0]
    normal diw
    exe "normal a" . curr_key
    exe "normal a "
    return 1
  endif
  return 0
endfunction
inoremap <silent> <C-L> <C-R>=(ExpandPossibleShorterSnippet() == 0? '': UltiSnips#ExpandSnippet())<CR>

this will make <CTRL-L> expand current snippet if there is no other snippet matching what you already wrote.

Second example uses another plugin, unite:

function! UltiSnipsCallUnite()
  Unite -start-insert -winheight=100 -immediately -no-empty ultisnips
  return ''
endfunction

inoremap <silent> <F12> <C-R>=(pumvisible()? "\<LT>C-E>":"")<CR><C-R>=UltiSnipsCallUnite()<CR>
nnoremap <silent> <F12> a<C-R>=(pumvisible()? "\<LT>C-E>":"")<CR><C-R>=UltiSnipsCallUnite()<CR>

If there is only one matching snippet it will work fine. If two snippets are present then a menu appears which gives you the option to choose from one of the matching options.

skeept
  • 12,077
  • 7
  • 41
  • 52
1

Ultisnips also offers an API usually mapped to <C-Tab> UltiSnips#ListSnippets(), you should use that in case you want to avoid typing the entire snippet name, although the chances of clashes will be more.

Dhruva Sagar
  • 7,089
  • 1
  • 25
  • 34
  • This is more like what snipMate does but I can't make it work. This is what I have in my .vimrc: let g:UltiSnipsListSnippets="" – Alejandro De Gregorio Apr 24 '14 at 12:53
  • 1
    @AlejandroDeGregorio note that depending on your terminal `` might not be recognized (it should be when using the GUI). Can you try to map it to another key and see if it works? – skeept Apr 24 '14 at 15:21
  • @skeept: Thanks, that was the problem. Now that I see how it shows the list it's not what I expected but it works. – Alejandro De Gregorio Apr 24 '14 at 17:06
  • @AlejandroDeGregorio you should try the second approach I suggest in my answer. I think the interface when you use unite plugin in nicer than the default list. – skeept Apr 24 '14 at 18:11
1

If you're just interested in matching either some or someTrigger, the simplest solution is probably to use a regex trigger:

snippet "some(Trigger)?" !r
blah blah
endsnippet

The !r identifies the trigger as a regex - note that a regex trigger must be enclosed in quotes. This will expand from either 'some' or 'someTrigger'.

See :help UltiSnips-adding-snippets

rcorre
  • 6,477
  • 3
  • 28
  • 33
  • Hey, thanks for your answer. I know it's not obvious from my question but I also want to match multiple triggers like someTrigger and someOtherTrigger. SnipMate shows a list of options just like the vim's omni-complete feature when that happen. I'm back using SnipMate now but I'll try UltiSnips again with more time to dig in. – Alejandro De Gregorio May 09 '14 at 15:41