hrsh7th / cmp-cmdline

nvim-cmp source for vim's cmdline
MIT License
493 stars 42 forks source link

Change select_next_item mapping #70

Open benbrastmckie opened 1 year ago

benbrastmckie commented 1 year ago

I am attempting to get <C-j> and <C-k> to be the means by which I navigate menu options throughout nvim. Currently Tab and <S-Tab> work when selecting from options in the command line or search. I have tried the following but without success:

-- cmp-cmdline
  -- `/` cmdline setup.
  cmp.setup.cmdline('/', {
    mapping = cmp.mapping.preset.cmdline({
      ['<C-j>'] = cmp.mapping(cmp.mapping.select_next_item()),
      ['<C-k>'] = cmp.mapping(cmp.mapping.select_prev_item()),
      ['<C-y>'] = cmp.config.disable, -- Specify `cmp.config.disable` if you want to remove the default `<C-y>` mapping.
    }),
    sources = {
      { name = 'buffer' }
    }
  })
  -- `:` cmdline setup.
  cmp.setup.cmdline(':', {
    mapping = cmp.mapping.preset.cmdline({
      ['<C-j>'] = cmp.mapping(cmp.mapping.select_next_item()),
      ['<C-k>'] = cmp.mapping(cmp.mapping.select_prev_item()),
      ['<C-y>'] = cmp.config.disable, -- Specify `cmp.config.disable` if you want to remove the default `<C-y>` mapping.
    }),
    sources = cmp.config.sources({
      { name = 'path' }
    }, {
      { name = 'cmdline',
        option = {
          ignore_cmds = { 'Man', '!' }
        }
      },
      mapping = cmp.mapping.preset.cmdline({}), -- fixes supertab
    }),
  })

I tried checking the mapping with :verbose cmap <C-j> and similarly for <C-k> and found that no mapping was found before or after including the config given above. Any help would be much appreciated!

AbelClapton commented 1 year ago

I'm having the same problem. I've tried several mapping configs but couldn't make it work

tmerse commented 5 months ago

Had the same issue. Looking at thecmp.mapping.preset.cmdline implementation (https://github.com/exosyphon/nvim/blob/2800baf59612b3a2498f9e764229fe05f6b1276a/after/plugin/lsp.lua#L63)

cmp.setup.cmdline('/', {
  mapping = cmp.mapping.preset.cmdline({
    ['<C-k>'] = {
      c = function()
        local cmp = require('cmp')
        if cmp.visible() then
          cmp.select_prev_item()
        else
          cmp.complete()
        end
      end,
    },
    ['<C-j>'] = {
      c = function()
        local cmp = require('cmp')
        if cmp.visible() then
          cmp.select_next_item()
        else
          cmp.complete()
        end
      end,
    },
  }),
  sources = {
    { name = 'buffer' }
  }
})

Did the trick for me (note cmp.select_next_item() vs cmp.mapping.select_next.item()