0

I'm trying to set up an autocompletion/snippet workflow that's a bit complicated. This is stemming from needing a better workflow for latex. Right now I'm using coc.nvim and UltiSnippets but I am willing from changing from coc to LanguageClient-neovim if need be.What I want to happen is as follows.

In general, I want tab to switch between things or scroll like it usually does with autocomplete, and I want enter to select things. I also want snippets to be prioritized.

Tab


If there is an autocomplete window open, I always want it to scroll through that. If a snippet is being expanded I want tab to jump to the next insert spot. Pseudo code - followed by what I think it roughly translates to - below.

if (there is an autocompletion context)
    scroll down to the next autocompletion option
elseif (this is in the middle of snippeting in select mode)
    jump to the next snippet insert place
else
    normal tab
endif
" --- roughly equivalent to ---
if pumvisible()
    <c-n>
elseif (this is in the middle of snippeting in select mode)
    idk
else
    idk
endif

Enter


If there is a snippet that can be expanded, expand the snippet. Otherwise if the autocompletion window is up, select whatever is there. If nothing special is going on, just a normal enter.

if (snippet can be expanded)
    (expand snippet) 
elseif (there is an autocompletion context)
    (select autocomplete option)
else
    (normal enter)
endif
" --- roughly equivalent to ---
if !empty(UltiSnips#SnippetsInCurrentScope())
    UltiSnips#ExpandSnippet()
elseif pumvisible()
    <c-y>
else
    <cr>
endif

I tried a lot of stuff with coc and ultisnips. I tried it with and without supertab. Supertab made it a lot better but it isn't quite what I want and some things, such as normal tabbing (sometimes) and jumping to the next snippet insert spot were broken.

Partial answers are welcome. @mods if this question is too specific or whatever, tell me how to break it up or make it better. The reason I ask is cause I figure it should be easy but I'm just missing something.

If there is some easy standard thing that I'm missing and its just like what I want but a little different, I'm open to it. If you think this is a dumb setup I'd love to learn why and explain my point of view.

  • I think it's possible, at least with neosnippet. Try to work it out with neosnippet and then port the workaround to ultisnips. – klaus Apr 7 at 8:00
0

I have my setup working the way you want with neovim and coc-vim. Here is what I have in my ~/.config/nvim/init.vim:

" Use tab for trigger completion with characters ahead and navigate.
" Use command ':verbose imap <tab>' to make sure tab is not mapped by other plugin.
function! s:check_back_space() abort
  let col = col('.') - 1
  return !col || getline('.')[col - 1]  =~# '\s'
endfunction
inoremap <silent><expr> <TAB>
      \ pumvisible() ? "\<C-n>" :
      \ <SID>check_back_space() ? "\<TAB>" :
      \ coc#refresh()
inoremap <expr><S-TAB> pumvisible() ? "\<C-p>" : "\<C-h>"

" Navigate snippet placeholders using tab
let g:coc_snippet_next = '<Tab>'
let g:coc_snippet_prev = '<S-Tab>'

" Use enter to accept snippet expansion
inoremap <expr> <cr> pumvisible() ? "\<C-y>" : "\<CR>"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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