6

I am trying to remap the autocomplete key from the "Enter" key to "TAB" because I keep autocompleting when I intend to go to the next line. The code below is the default option for coc, and I think this is where I should be able to remap the key.

" make <CR> auto-select the first completion item and notify coc.nvim to
" format on enter, <cr> could be remapped by other vim plugin
inoremap <silent><expr> <cr> pumvisible() ? coc#_select_confirm()
                              \: "\<c-g>u\<CR>\<c-r>=coc#on_enter()\<CR>"

I thought that changing the <cr> in the beginning to <TAB> would work. However, although it does allow me to autocomplete using TAB, it creates weird auto indentations in some cases. For example:

//normal behavior
someFunction(){
    //cursor here appropriately indented
}


//behavior after I made the changes mentioned above
someFunction(){
//cursor here}

I assume I just fundamentally don't understand something about coc or remapping keys in VIM.

Why can't I simply change that <cr> to <TAB>? How can I go about remapping the autocomplete key from "Enter" to "TAB"?

christofuy
  • 131
  • 3
  • 7
  • I don't know if that's any help, but here's the bit of my vimrc dedicated to coc completion: https://codesandbox.io/s/falling-butterfly-yejn1?file=/init.vim – Zorzi Dec 22 '20 at 12:13

2 Answers2

5

I don't understand vimscript too well, but I managed to get something working by trial and error.

Default Setting:

inoremap <silent><expr> <cr> pumvisible() ? coc#_select_confirm()
                              \: "\<C-g>u\<CR>\<c-r>=coc#on_enter()\<CR>"

Autocompleting on Tab:

"This expression seems to be responsible for coc formatting on enter
inoremap <silent><expr> <cr> "\<C-g>u\<CR>\<c-r>=coc#on_enter()\<CR>"
"I this just says autocomplete with the first option if pop up menu is open.
"If it is not open, just do a regular tab.
inoremap <silent><expr> <tab> pumvisible() ? coc#select_confirm() : "\<C-g>u\<tab>"

Note: Using coc#pum#confirm() instead of coc#select_confirm() works and is what the coc official documentation in git uses (reviewed on May-2023)

greybeard
  • 2,249
  • 8
  • 30
  • 66
christofuy
  • 131
  • 3
  • 7
  • Is it supposed to be `coc#_select_confirm` for the last line? – sguan Jan 10 '21 at 06:44
  • @sguan The answer I posted has been working for me and lets me autocomplete on TAB instead of on ENTER. I believe `coc#_select_confirm` is the function responsible for executing the autocompletion. So what I think the last line is saying on a high level is: if the autocomplete pop-up menu is open, then perform autocompletion, else do a regular TAB. – christofuy Jan 11 '21 at 00:35
  • This is wrong. It should be `_coc#_select_confirm` like @sguan said – Natan Fernandes Mar 03 '22 at 22:06
5

Replace the following line from the example coc config

inoremap <silent><expr> <CR> coc#pum#visible() ? coc#pum#confirm()
                              \: "\<C-g>u\<CR>\<c-r>=coc#on_enter()\<CR>"

with this:

inoremap <silent><expr> <TAB> coc#pum#visible() ? coc#pum#confirm() : "\<C-g>u\<TAB>"

This was based on @christofuy's answer but updated after this line changed.

Jon Deaton
  • 3,943
  • 6
  • 28
  • 41