1

I'm using coc.nvim for autocomplete and when the popup menu appears, I'd like Tab to select the first item and close the menu. At the moment I've keybinded Tab to <C-n><CR>, but the <CR> actually puts in a line return which is not what I want.

inoremap <expr> <Tab> pumvisible() ? "\<C-n><Space>" : "<Tab>"
doctopus
  • 5,349
  • 8
  • 53
  • 105

3 Answers3

2

I found the answer the coc.nvim's example docs:

https://github.com/neoclide/coc.nvim/blob/e1a4ce4d95d1d89b6dd31019cc4387425aa09b86/doc/coc.txt#L892-L909

inoremap <silent><expr> <TAB>
      \ pumvisible() ? coc#_select_confirm() :
      \ coc#expandableOrJumpable() ?
      \ "\<C-r>=coc#rpc#request('doKeymap', ['snippets-expand-jump',''])\<CR>" :
      \ <SID>check_back_space() ? "\<TAB>" :
      \ coc#refresh()

    function! s:check_back_space() abort
      let col = col('.') - 1
      return !col || getline('.')[col - 1]  =~# '\s'
    endfunction

    let g:coc_snippet_next = '<tab>'

Works like an absolute charm.

doctopus
  • 5,349
  • 8
  • 53
  • 105
  • Tabs work for autocompletion, but dont work as normal for creating tabs. I get the following error each time i press tab: "Error on request (snippetcheck): Vim(return) :Error invoking 'snippitCheck' on channel 3 (coc)..." – Narayana Oct 22 '22 at 14:43
1

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

fannheyward
  • 18,599
  • 12
  • 71
  • 109
1

After struggling for so long and trying these things I realised it works out the box fine.

To select the first item in the dropdown list press: ctrl+y.

Press: ctrl+n select the next item in the drop down list.

Press: ctrl+p to select the previous item in the drop down list.

To use tab and shift tab as well to navigate next and previous items see the documentation.

To use tab to select first item in the drop down list below the above documentations config:

inoremap <expr> <TAB> pumvisible() ? "\<C-y>" : "\<C-g>u\<TAB>"
Narayana
  • 323
  • 3
  • 16