ge0rg
ge0rg

Reputation: 73

Vim with Ultisnips and bracket-matching (auto-pairs) mapped to the same key

I use vim mainly for latex. The main reason why I do it is actually Ultisnips, which greatly reduces the number of symbols that I need to type in latex. Alongside it, I use auto-pairs, which matches brackets for me. One small gripe that I have with this setup is that I cannot use tab in order to both expand a snippet and jump outside of a pair of brackets - the relevant options are:

let g:UltiSnipsExpandTrigger = '<tab>'
let g:UltisnipsJumpForwardTrigger = '<tab>'
let g:AutoPairsShortcutJump = '<tab>'

The behavior that I would like is to expand a snippet if one is available, otherwise jump out of the bracket. But that does not work by default. Is there anything I can do, like some kind of conditional key mapping?

I have tried other solutions, like implementing bracket matching in Ultisnips for example, but never with satisfactory results.

Yes, I could simply use another key, but using tab for both would be the most convenient.

Upvotes: 1

Views: 773

Answers (1)

Das_Geek
Das_Geek

Reputation: 2845

This can be done with user functions, utilizing the return values of the UltiSnips Trigger functions. Since you have everything mapped to the same key, I'll create an example using the combined UltiSnips function, ExpandSnippetOrJump, which sets the global variable ulti_expand_or_jump_res to 0 if it could neither expand a snippet nor jump to the next one.

So you'll define a function (cheekily named Ulti_Pairs()) with:

function! Ulti_Pairs()
    call UltiSnips#ExpandSnippetOrJump()
    if g:ulti_expand_or_jump_res == 0
        call AutoPairsJump()
    endif
endfunction

Then you'll set your mapping (you shouldn't need your other ones after this):

inoremap <Tab> <ESC>:call Ulti_Pairs()<CR>a

So to walk through what this is doing, when you press Tab Vim will call your custom function, which in turn calls ExpandSnippetOrJump(). If that function, returns anything else but 0 (meaning there was something to expand or jump to in UltiSnips) it won't do anything more. But on a return value of 0, it'll call the AutoPairsJump() function.

Upvotes: 2

Related Questions