Replies: 4 comments 8 replies
-
I think you can use the event, see Something like this cmp.event:on('menu_opened', function()
cmp.confirm({ select = true })
end) |
Beta Was this translation helpful? Give feedback.
-
The solution below works just as I wanted. I can hit Tab to trigger a completion with the first result selected and inserted. If I want that result I can just keep typing the next symbol and the menu closes. If I don't want that item I can continue to hit Tab to move down the list. I'll likely include a cancel key and such but this alone works for most cases that interest me. One key to complete in most cases and no menus popping up unbidden. This is the flow for me! Thanks for the help. local has_words_before = function()
unpack = unpack or table.unpack
local line, col = unpack(vim.api.nvim_win_get_cursor(0))
return col ~= 0
and vim.api
.nvim_buf_get_lines(0, line - 1, line, true)[1]
:sub(col, col)
:match("%s")
== nil
end
local cmp = require "cmp"
cmp.setup {
completion = {
autocomplete = false,
completeopt = "menu,menuone,noselect",
},
snippet = {
expand = function(args)
vim.fn["vsnip#anonymous"](args.body)
end,
},
mapping = {
["<Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_next_item()
elseif has_words_before() then
cmp.complete()
else
fallback()
end
end, { "i", "s" }),
},
sources = cmp.config.sources({
{ name = "buffer" },
}),
}
cmp.event:on("menu_opened", function()
-- insert a Tab key to select and insert the first result
vim.api.nvim_feedkeys('\t', 'i', false)
end) |
Beta Was this translation helpful? Give feedback.
-
Oh! I also need this feature, or similar, yesterday I opened an issue about that: #1529 |
Beta Was this translation helpful? Give feedback.
-
@gbishop I think I found the solution: #1529 (comment) Tell me if you have an issue setting it up so I can share you my full conf so you can compare |
Beta Was this translation helpful? Give feedback.
-
I'd like to manually trigger the completion menu and have the first result both selected and inserted. I can get it selected but I can't get that first result automatically inserted.
I've mapped the TAB key to trigger the menu. In my dreams I'd be typing along and hit TAB to have the correct result appear with no other keys required. My current configuration requires that I hit TAB twice; once to open the menu and once to select and insert the result.
EDIT: I don't simply want to insert the first choice and be done with it; I want to reserve the possibility of hitting TAB again to select a later result. It would work just like the two TAB sequence but with only 1 TAB.
How can I open the menu and both select and insert the first item with a single key?
I included the minimal configuration below. Thanks for any suggestions.
Beta Was this translation helpful? Give feedback.
All reactions