Skip to content

Instantly share code, notes, and snippets.

@alexcg1
Created January 22, 2025 13:02
Show Gist options
  • Select an option

  • Save alexcg1/46cded2d8ddafca24646003725e5fd60 to your computer and use it in GitHub Desktop.

Select an option

Save alexcg1/46cded2d8ddafca24646003725e5fd60 to your computer and use it in GitHub Desktop.
Disable <Enter> select top autocomplete option in nvim-cmp
-- store in ~/.config/nvim/lua/plugins/cmp.lua
local cmp = require("cmp")
local cmp_active = true
local function toggle_cmp(enable)
if enable then
cmp_active = true
cmp.setup.buffer({ enabled = true })
else
cmp_active = false
cmp.setup.buffer({ enabled = false })
end
end
-- Re-enable cmp when entering insert mode
vim.api.nvim_create_autocmd("InsertEnter", {
callback = function()
-- "try" to swallow error when not in a normal buffer (e.g., TelescopePrompt etc.)
pcall(function()
toggle_cmp(true)
end)
end,
})
return {
"hrsh7th/nvim-cmp",
opts = {
mapping = {
-- Make it so suggestions must be selected to confirm with Enter.
-- Allows Enter to be used for newlines.
["<CR>"] = cmp.mapping({
i = function(fallback)
if cmp.visible() and cmp.get_active_entry() then
cmp.confirm({ behavior = cmp.ConfirmBehavior.Replace, select = false })
else
fallback()
end
end,
}),
-- Map Ctrl+Enter to confirm active entry.
["<C-CR>"] = cmp.mapping.confirm({ select = true }),
-- Map Ctrl+Space to close cmp menu instead, so you don’t have to Ctrl+C twice.
-- Closes cmp menu until InsertEnter if active, or re-enables cmp menu.
["<C-Space>"] = cmp.mapping(function(fallback)
if cmp_active then
if cmp.visible() then
cmp.close()
toggle_cmp(false)
else
fallback()
end
else
toggle_cmp(true)
end
end, { "i" }),
-- Additional mappings can go here.
...,
},
},
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment