Comments (10)
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.
@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" },
}),
})
from cmp-cmdline.
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
from cmp-cmdline.
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.
from cmp-cmdline.
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!
from cmp-cmdline.
I use <Tab>
and <S-Tab>
. I thought I mentioned that above, but I guess I didn't.
from cmp-cmdline.
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" },
}),
})
from cmp-cmdline.
@plt3 Thanks a lot, this will slim down my init.lua
a bit :))
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 🙃).
from cmp-cmdline.
@plt3 @maciejzj guys, you're the best,
Also, your solution have these attrs:
- supports all three
:
,?
,/
, not just:
- preset keymaps doesn't override Up&Down arrows, so it supports Up, Down arrows to search history based on history filtered by already entered substring: i.e.
:echo
pressUp
to search through history of commands that starts withecho
.
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" },
}),
})
from cmp-cmdline.
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.
from cmp-cmdline.
@yangmillstheory, how do you cycle backwards and forwards now? With the mapping proposed by @adinhodovic, I can't do that anymore.
from cmp-cmdline.
Related Issues (20)
- Error when calling `vim.fn.getcompletion()` for cmdline has unfinished regex patterns
- FR: extends command items that not really exists HOT 1
- Problem in latex files
- Feature request: lua LSP autocompletion on `cmp-cmdline`
- Respect wildignore when completing files
- Ignore :w !sudo HOT 2
- Can you hide source? HOT 1
- cmp-cmdline causes Neovim to freeze when using ! in command mode HOT 3
- Completion for path with spaces failing
- Error detected while processing TextChangedI Autocommands for "*": HOT 2
- Not consistent { autocomplete = false } HOT 1
- Wildcard not substituted properly when using regex in :find command HOT 1
- Next/previous works for command and search completion, but not in substitute command HOT 4
- Abort nvim when using `:tag` HOT 1
- The cmp view is not close when enter cmdwin. HOT 1
- Variable type? HOT 14
- first result auto-selected, but it doesn't search for the text in that result until I move selection down and back up again HOT 4
- ...e/pc/.local/share/nvim/lazy/nvim-cmp/lua/cmp/view.lua:102: attempt to call local 'fn' (a table value) HOT 1
- Numeric completion completes all possibilities when previously it did not HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cmp-cmdline.