2

I just started using coc.nvim with coc-pyright on Vim 8.2.5050. In the GitHub readme of the coc.nvim plugin, I found instructions to get autocomplete working with the Tab key.

While this is working well, I noticed, that the Tab completion isn't adding parentheses to autocompleted functions. However, the autocompletion with the Enter key does add the function parentheses.

Is there anyway to get this same functionality working with the Tabautocompletion?

This is what I copied from the coc.nvim GitHub readme to my vimrc:

" Use tab for trigger completion with characters ahead and navigate.
" NOTE: Use command ':verbose imap <tab>' to make sure tab is not mapped by
" other plugin before putting this into your config.
inoremap <silent><expr> <TAB>
      \ pumvisible() ? "\<C-n>" :
      \ CheckBackspace() ? "\<TAB>" :
      \ coc#refresh()
inoremap <expr><S-TAB> pumvisible() ? "\<C-p>" : "\<C-h>"

function! CheckBackspace() abort
  let col = col('.') - 1
  return !col || getline('.')[col - 1]  =~# '\s'
endfunction
4
  • I believe coc pairs is what you are looking for.
    – unrealapex
    Jun 15, 2022 at 5:56
  • 1
    I already have coc-pairs installed and it automatically closes parentheses just fine if I type them myself. What I want is them to automatically appear after tab-completing a python function call for example.
    – FAB
    Jun 15, 2022 at 21:24
  • 1
    Try mapping <kbd>Tab</kbd> to CoC's accept completion function. I am not aware what it specifically is so I will take a look at Coc's documentation and get back to you.
    – unrealapex
    Jun 15, 2022 at 22:46
  • 1
    Update: I have found the function, it is coc#_select_confirm(). I have created a mapping with it in an answer below.
    – unrealapex
    Jun 15, 2022 at 23:15

1 Answer 1

3

To have Tab trigger completion like Enter, bind it to coc#_select_confirm()(CoC's accept completion function):

inoremap <expr> <Tab> pumvisible() ? coc#_select_confirm() : "\<Tab>"

This bind only controls Tab when the CoC completion menu is open.

Since Tab is no longer used to quickly go down the completion list, you can use Ctrl + N and Ctrl + P to traverse the completion list. They are the keys that are default for traversing completion lists.

5
  • 1
    It works perfectly fine in the way you described it. But what I actually want to achieve is to complete with tab like your mapping does, but also the ability to keep cycling through suggestions with tab. I don't know if this is possible at all.
    – FAB
    Jun 18, 2022 at 6:53
  • Yeah, unfortunately it is not. You could bind the keys that go up and down the completion list(Ctrl + N and Ctrl + P) to different keys. Binding it to Alt + J and Alt + K or Ctrl + J and Ctrl + K(I use this and Alt + J/K for a bind I have set to something else) work.
    – unrealapex
    Jun 18, 2022 at 8:56
  • 1
    See the updated answer above. If this solves your problem, perhaps mark it as the answer?
    – unrealapex
    Jun 18, 2022 at 9:00
  • Sorry, didn't get around to test it yet because I'm travelling right now. But I think this pretty much should solve everything. As soon as I got around testing it I will mark the question as solved by your answer
    – FAB
    Jun 21, 2022 at 11:37
  • No worries, stay safe and have fun!
    – unrealapex
    Jun 21, 2022 at 19:23

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.