8

I'm using nvim-cmp as my completion engine, which is working fine, but would like to change the default behavior to disable the automatic selection of the first option. The reason is that, when the last word of a line has suggestions, pressing enter will apply the first suggestion instead of just inserting a newline.

For example, in haskell, typing

main = do<CR>

the do matches diso~ from luasnip, and is replaced by something like

main = 2022-12-05T12:50:34

I would prefer the suggestions to be visible but none of them selected until tab is pressed, and if none is selected then <CR> is just a newline. Is this possible?

Miguel
  • 658
  • 1
  • 6
  • 19
  • Run command `set completeopt?`, what is the output? Try to use `set completeopt=menu,menuone,noselect` to see if it works as expected. – jdhao Dec 06 '22 at 02:27
  • @jdhao it's `completeopt=menu,preview,menuone,noselect` but changing it seems to have no effect. – Miguel Dec 06 '22 at 13:49

2 Answers2

8

Answering my own question, I found out that when using lsp-zero, the configuration has to be done there. The documention in advanced-usage.md provides the exact solution, which I'm posting here:

local lsp = require('lsp-zero')
lsp.preset('system-lsp')  -- or recommended, or whatever...
lsp.setup_nvim_cmp({
    preselect = 'none',
    completion = {
        completeopt = 'menu,menuone,noinsert,noselect'
    },
})
lsp.setup()
Miguel
  • 658
  • 1
  • 6
  • 19
1

First set completeopt=menu,preview,menuone,noselect to configure auto-completion menu.

Then you have to modify your cmp plugin configuration for mapping of CR key :

local cmp = require'cmp'

cmp.setup({
  mapping = cmp.mapping.preset.insert({
    ['<CR>'] = cmp.mapping.confirm({ select = false }),
  })
})

See cmp plugins documentation for further explanations about cmp.confirm option.

lcheylus
  • 1,217
  • 8
  • 21
  • 2
    Thanks, but this has no effect for my configuration. I figured out that `lsp-zero` was the culprit. There is a configuration example in file `advanced-usage.md` from `lsp-zero` that works exactly as intended. – Miguel Dec 08 '22 at 13:14