Skip to main content
Format Lua code and fix title
Source Link
lcheylus
  • 2.7k
  • 2
  • 19
  • 35

Auto seletionselection of first entry in auto-suggestion from nvim-cmp

I have this nvim-cmpconfiguration 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,
}
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,
}
  • I have tried adding noinsertnoinsert in completion
  • Added rule for enter( one)
  • Added rule for auto pressing up key as it cancels auto selection

Auto seletion of first entry in auto-suggestion from nvim-cmp

I have this nvim-cmp file

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,
}
  • I have tried adding noinsert in completion
  • Added rule for enter( one)
  • Added rule for auto pressing up key as it cancels auto selection

Auto selection of first entry in auto-suggestion from nvim-cmp

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,
}
  • I have tried adding noinsert in completion
  • Added rule for enter( one)
  • Added rule for auto pressing up key as it cancels auto selection
Source Link
Bruce
  • 11
  • 1

Auto seletion of first entry in auto-suggestion from nvim-cmp

I have this nvim-cmp file

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