Miguel
Miguel

Reputation: 796

make nvim-cmp not autoselect the 1st option

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?

Upvotes: 15

Views: 9946

Answers (3)

lcheylus
lcheylus

Reputation: 2423

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.

Upvotes: 5

Flimm
Flimm

Reputation: 151133

If you would like to prevent tab from selecting the first autocomplete suggestion in the command line (after pressing :), then this is how. You need to disable the functionality of the tab and shift+tab keys altogether:

cmp.setup.cmdline(':', {
  mapping = cmp.mapping.preset.cmdline({
   ['<Tab>'] = { c = function() end },
   ['<S-Tab>'] = { c = function() end },
  }),
  sources = cmp.config.sources({
    { name = 'path' }
  }, {
    { name = 'cmdline' }
  })
})

You can find the default mapping returned by cmp.mapping.preset.cmdline() by looking at the file lua/cmp/config/mapping.lua in the nvim-cmp directory.

Upvotes: 0

Miguel
Miguel

Reputation: 796

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()

Upvotes: 14

Related Questions