-
Notifications
You must be signed in to change notification settings - Fork 48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can't cycle through history with Ctrl-N and Ctrl-P in command-line mode #108
Comments
I just did disabled the built in mappings for the cmdline preset. cmp.setup.cmdline(":", {
mapping = cmp.mapping.preset.cmdline({
-- Use default nvim history scrolling
["<C-n>"] = {
c = false,
},
["<C-p>"] = {
c = false,
},
}),
sources = cmp.config.sources({
{ name = "path" },
}, {
{ name = "cmdline" },
}),
}) Might solve ur usecase. Nvim Config |
Thanks @adinhodovic, this looks like it works with no observable downsides. I wonder what the intent of the C-n / C-p mappings that we're overriding were? Because I guess the downside of the override, is that we're losing whatever benefit would be otherwise provided. So far I can't find any. |
I think they're used for scrolling up and down in the completion list, why I'm not sure. |
I see. In that case I'm still able to use and to cycle forwards and backwards through the completion list, so no harm done. I do think this is still a bug that should be resolved. Thanks! |
@yangmillstheory, how do you cycle backwards and forwards now? With the mapping proposed by @adinhodovic, I can't do that anymore. |
I use |
I've faced a similar problem, but in a slightly different context. I'm used to navigating history with C-N/C-P and also using them for selecting completions in vim popup windows. So, I developed a solution that lets me do both simultaneously. Here's the trick: With the code snippet below, C-N/C-P will navigate through the command line history (like in shell), unless you start typing a character. When you start typing, C-N/C-P switches to selecting completions instead of navigating history. This way, you get the best of both worlds. The may not be the setup OP is looking for, however, someone may find it useful one day, so I decided to share it here :) -- For command line
-- This custom mappig setup causes CTRL-P, CTRL-N to fallback to history
-- browsing, unless user has explicitly typed something in the cmdline, then
-- these two activate to browse completion options.
local cmdline_cmp_state = "has_not_typed"
vim.api.nvim_create_autocmd({ "CmdlineEnter" }, {
command = "lua cmdline_cmp_state = 'has_not_typed'",
})
vim.api.nvim_create_autocmd({ "CmdlineChanged" }, {
callback = function()
if cmdline_cmp_state == "has_not_typed" then
cmdline_cmp_state = "has_typed"
elseif cmdline_cmp_state == "has_browsed_history" then
cmdline_cmp_state = "has_not_typed"
end
end,
})
local function select_or_fallback(select_action)
return cmp.mapping(function(fallback)
if cmdline_cmp_state == "has_typed" and cmp.visible() then
select_action()
else
cmdline_cmp_state = "has_browsed_history"
cmp.close()
fallback()
end
end, { "i", "c" })
end
cmp.setup.cmdline(":", {
mapping = cmp.mapping.preset.cmdline({
["<C-n>"] = select_or_fallback(cmp.select_next_item),
["<C-p>"] = select_or_fallback(cmp.select_prev_item),
}),
sources = cmp.config.sources({
{ name = "path" },
}, {
{ name = "cmdline" },
}),
}) |
@maciejzj, I was facing this exact same issue and saw your solution, which works great! However, after some digging around I found a simpler configuration that achieves the same thing (for me, at least). Posting it here if it can be useful: -- Use cmdline & path source for ':'
cmp.setup.cmdline(":", {
-- C-n/C-p cycle through completions if a character has been typed and through
-- command history if not (from https://www.reddit.com/r/neovim/comments/v5pfmy/comment/ibb61w3/)
mapping = cmp.mapping.preset.cmdline({
["<C-n>"] = { c = cmp.mapping.select_next_item() },
["<C-p>"] = { c = cmp.mapping.select_prev_item() },
}),
sources = cmp.config.sources({
{ name = "path" },
}, {
{ name = "cmdline" },
}),
}) |
@plt3 Thanks a lot, this will slim down my It would be great to see this setup in some more visible place like the README. I guess many users would want this behaviour and it seems rather unobvious how to achieve it (TBH I would have never figured out that specifying that next/prev should explicitly work in "command mode" would result in this behaviour overall and still don't fully get it 🙃). |
@plt3 @maciejzj guys, you're the best, Also, your solution have these attrs:
I will also share my snippet that I based on your work: I don't use preset, only set maps that I use. -- Declare only keys that I actually use
-- Based on https://github.com/hrsh7th/cmp-cmdline/issues/108#issuecomment-2052449375
-- C-n/C-p cycle through completions if a character has been typed and through
-- command history if not (from https://www.reddit.com/r/neovim/comments/v5pfmy/comment/ibb61w3/)
local cmd_mapping = {
["<C-Space>"] = { c = cmp.mapping.complete({}) },
["<C-n>"] = { c = cmp.mapping.select_next_item() },
["<C-p>"] = { c = cmp.mapping.select_prev_item() },
["<C-e>"] = { c = cmp.mapping.abort() },
["<C-y>"] = {
c = cmp.mapping.confirm({
behavior = cmp.ConfirmBehavior.Insert,
select = true,
}),
},
}
-- Use buffer source for `/` and `?`
cmp.setup.cmdline({ "/", "?" }, {
mapping = cmd_mapping,
sources = {
{ name = "buffer" },
},
})
-- Use cmdline & path source for ':'
cmp.setup.cmdline(":", {
mapping = cmd_mapping,
sources = cmp.config.sources({
{ name = "path" },
}, {
{ name = "cmdline" },
}),
}) |
Wow @plt3 I cannot tell you how much you saved me here! I have been trying to figure this out off and on for weeks! For me it was not being able to use the Up / Down arrows to cycle through "/" search history. Your suggestion allowed me to figure it out with this below! I still don't quite understand why this works! I thought select_next_item() would do exactly what I was trying to prevent... opening the cmp menu and selecting the next completion suggestion! But if it works, it works! Thanks! mapping = cmp.mapping.preset.cmdline({
["<Up>"] = { c = cmp.mapping.select_next_item() },
["<Down>"] = { c = cmp.mapping.select_prev_item() },
}), |
Hello,
I can't use Ctrl-P to cycle through my ex-command history, I think due to some poor interaction with this plugin. For example, when type
:
and then repeatedly hitCtrl-P
, this happens:Screen.Recording.2024-01-29.at.22.10.29.mov
When I comment out the
cmdline
source (see the top right pane), instead I can do:Screen.Recording.2024-01-29.at.22.11.56.mov
Here's some information about my setup:
Also here's the result of
:checkhealth
:When I run
:cmap
, it looks like the binding comes fromnvim-cmp
:However, commenting out the
cmdline
source fixes the issue, so I'm filing the bug here. Please let me know how I can help.The text was updated successfully, but these errors were encountered: