Code Monkey home page Code Monkey logo

Comments (10)

plt3 avatar plt3 commented on July 30, 2024 2

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.

adinhodovic avatar adinhodovic commented on July 30, 2024 1

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.

yangmillstheory avatar yangmillstheory commented on July 30, 2024 1

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.

yangmillstheory avatar yangmillstheory commented on July 30, 2024 1

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.

yangmillstheory avatar yangmillstheory commented on July 30, 2024 1

I use <Tab> and <S-Tab>. I thought I mentioned that above, but I guess I didn't.

from cmp-cmdline.

maciejzj avatar maciejzj commented on July 30, 2024 1

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.

maciejzj avatar maciejzj commented on July 30, 2024 1

@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.

aimestereo avatar aimestereo commented on July 30, 2024 1

@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 press Up to search through history of commands that starts with echo .

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.

adinhodovic avatar adinhodovic commented on July 30, 2024

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.

pogopaule avatar pogopaule commented on July 30, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.