6

Recently I rewrote my NeoVim config file from vim script to lua and everything is working fine, but I have one issue. When I'm using coc.nvim for autocompletion and I select something, hit enter, NeoVim makes a new line and doesn't autocomplete. I found the solution there, but only for Vim Script. How can I do this in NeoVim Lua?

inoremap <expr> <CR> pumvisible() ? "\<C-Y>" : "\<CR>"
romainl
  • 23,415
jihndoc
  • 61

2 Answers2

10

This is a quite easy you just have to add:

inoremap <silent><expr> <CR> coc#pum#visible() ? coc#pum#confirm() : "\<CR>"

you have to put this line inside init.lua

However, If you use the auto paring package (https://github.com/windwp/nvim-autopairs) with COC it's not going to work.

to check whether it's going to work or not simple run

:verbose imap <CR> 

after you install and setup your package

If nothing is there you are good to go and use my imap that I provide. If there is any key map that is already binding to the CR (which means enter BTW) then, you have to remap it on the package or use a different key than enter.

In my case, windwp auto paring takes the enter key and that leads to an error.

The solution is to use COC auto pair. just run :CocInstall coc-pairs and you are good to go.

1

I think i find the solution!

If you install the plugin via packer, in your init.lua write:

require('nvim-autopairs').setup{
  map_cr = false,
}

I hope it works for you