1

I have this configuration file for nvim-cmp plugin:

return {
  'hrsh7th/nvim-cmp',
  event = 'InsertEnter',
  dependencies = {
    'hrsh7th/cmp-buffer',
    'hrsh7th/cmp-path',
    'hrsh7th/cmp-nvim-lsp',
    {
      'L3MON4D3/LuaSnip',
      version = 'v2.*',
      build = 'make install_jsregexp',
    },
    'saadparwaiz1/cmp_luasnip',
    'rafamadriz/friendly-snippets',
    'onsails/lspkind.nvim',
  },
  config = function()
    local cmp = require('cmp')
    local luasnip = require('luasnip')
    local lspkind = require('lspkind')
    require('luasnip.loaders.from_vscode').lazy_load()
    -- Disable preselection at LSP capabilities level
    local capabilities = require('cmp_nvim_lsp').default_capabilities()
    capabilities.completion.completionItem.preselectSupported = false
    cmp.setup({
      preselect = cmp.PreselectMode.None,
      completion = { completeopt = 'menu,menuone,preview,noselect,noinsert' },
      snippet = {
        expand = function(args)
          luasnip.lsp_expand(args.body)
        end,
      },
      mapping = {
        ['<C-n>'] = cmp.mapping.select_next_item(
          { behavior = cmp.SelectBehavior.Select },
          { desc = 'Select next item' }
        ),
        ['<C-p>'] = cmp.mapping.select_prev_item(
          { behavior = cmp.SelectBehavior.Select },
          { desc = 'Select previous item' }
        ),
        ['<C-b>'] = cmp.mapping.scroll_docs(-4, { desc = 'Scroll docs up' }),
        ['<C-f>'] = cmp.mapping.scroll_docs(4, { desc = 'Scroll docs down' }),
        ['<C-Space>'] = cmp.mapping.complete({ desc = 'Trigger completion' }),
        ['<C-e>'] = cmp.mapping.abort({ desc = 'Abort completion' }),
        ['<CR>'] = cmp.mapping(function(fallback)
          if cmp.visible() and cmp.get_selected_entry() then
            cmp.confirm({ select = false })
          else
            fallback()
          end
        end, { 'i', 's', desc = 'Confirm selection' }),
        ['<Tab>'] = cmp.mapping(function(fallback)
          if cmp.visible() then
            cmp.select_next_item()
          else
            fallback()
          end
        end, { 'i', 's', desc = 'Next item or indent' }),
        ['<S-Tab>'] = cmp.mapping(function(fallback)
          if cmp.visible() then
            cmp.select_prev_item()
          else
            fallback()
          end
        end, { 'i', 's', desc = 'Previous item or dedent' }),
      },
      sources = cmp.config.sources({
        { name = 'nvim_lsp' },
        { name = 'luasnip' },
        { name = 'buffer' },
        { name = 'path' },
      }),
      formatting = {
        format = lspkind.cmp_format({
          maxwidth = 50,
          ellipsis_char = '...',
        }),
      },
    })
    -- Automatically deselect first item when menu opens
    cmp.event:on('menu_opened', function()
      vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes('<Up>', true, true, true), 'n', true)
    end)
    -- Pass capabilities to your LSP servers (add this to your lspconfig setup)
    -- Example: require("lspconfig").lua_ls.setup({ capabilities = capabilities })
  end,
}

What I have tried :

  • I have tried adding noinsert in completion
  • Added rule for enter( one)
  • Added rule for auto pressing up key as it cancels auto selection

if there something else I have to provide feel free to say so Your help is much appreciated.

Auto completion to select word when pressed tab or something

1
  • 1
    Hello. But what is your question? Do you want to enable auto selection of first item, or do you want to disable auto selection of first item? Commented Oct 20 at 9:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.