hrsh7th / nvim-cmp

A completion plugin for neovim coded in Lua.
MIT License
7.88k stars 393 forks source link

Unable to set custom key mappings for command mode autocompletion #1835

Open pwnalone opened 7 months ago

pwnalone commented 7 months ago

FAQ

Announcement

Minimal reproducible full config

if has('vim_starting')
  set encoding=utf-8
endif
scriptencoding utf-8

if &compatible
  set nocompatible
endif

let s:plug_dir = expand('/tmp/plugged/vim-plug')
if !filereadable(s:plug_dir .. '/plug.vim')
  execute printf('!curl -fLo %s/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim', s:plug_dir)
end

execute 'set runtimepath+=' . s:plug_dir
call plug#begin(s:plug_dir)
Plug 'hrsh7th/nvim-cmp'
Plug 'hrsh7th/cmp-buffer'
Plug 'hrsh7th/cmp-cmdline'
Plug 'hrsh7th/cmp-nvim-lsp'
Plug 'hrsh7th/vim-vsnip'
Plug 'neovim/nvim-lspconfig'
call plug#end()
PlugInstall | quit

lua << EOF
local cmp = require "cmp"
cmp.setup {
  snippet = {
    expand = function(args)
      vim.fn["vsnip#anonymous"](args.body)
    end,
  },

  -- XXX: This works...
  mapping = cmp.mapping.preset.insert({
    ['<C-j>'] = cmp.mapping.select_next_item(),
    ['<C-k>'] = cmp.mapping.select_prev_item(),
  }),

  sources = cmp.config.sources({
    { name = "nvim_lsp" },
    { name = "buffer" },
  }),
}

cmp.setup.cmdline(":", {
  -- XXX: But this doesn't.
  mapping = cmp.mapping.preset.cmdline({
    ['<C-j>'] = cmp.mapping.select_next_item(),
    ['<C-k>'] = cmp.mapping.select_prev_item(),
  }),

  sources = cmp.config.sources({
    { name = "cmdline" },
  }),
})
EOF

lua << EOF
local capabilities = require('cmp_nvim_lsp').default_capabilities()

require'lspconfig'.cssls.setup {
  capabilities = capabilities,
}
EOF

Description

It's possible to set custom key mappings for insert mode like so.

mapping = cmp.mapping.preset.insert({
  ['<C-j>'] = cmp.mapping.select_next_item(),
  ['<C-k>'] = cmp.mapping.select_prev_item(),
})

But doing the same for command mode does not work.

mapping = cmp.mapping.preset.cmdline({
  ['<C-j>'] = cmp.mapping.select_next_item(),
  ['<C-k>'] = cmp.mapping.select_prev_item(),
})

Steps to reproduce

  1. Restart Neovim after installing plugins via the included config.
  2. Enter command mode and type partial command (e.g. :h).
  3. Hit <C-j> or <C-k> to navigate the list of completion suggestions.
  4. Observe that it does not work.

Expected behavior

Adding custom key mappings for command mode autocompletion should work just like it does for insert mode autocompletion.

Actual behavior

Adding custom key mappings for command mode autocompletion does not work.

Additional context

As a temporary workaround, the following appears to work.

mapping = cmp.mapping.preset.cmdline({
  ['<C-j>'] = { c = cmp.mapping.select_next_item() },
  ['<C-k>'] = { c = cmp.mapping.select_prev_item() },
})