AckslD / nvim-neoclip.lua

Clipboard manager neovim plugin with telescope integration
931 stars 19 forks source link

Allow for visual selection override #128

Open peterfication opened 2 weeks ago

peterfication commented 2 weeks ago

I often select a certain word and want to replace it with a paste. Unfortunately, I can't use neoclip properly yet when doing this because it says "No visual range allowed".

I could fake it with a visual keymap to d:Telescope neoclip<CR>, which first deletes the selected word and then opens Neoclip. However, if I cancel, the word will be deleted anyways.

Is it somehow possible to add a visual range? Or is it too complex?

AckslD commented 2 weeks ago

I think it should be possible to add, just hasn't been done :) I'm not sure I'll have time in the near future so if anyone wants to take a stab at this feel free to :)

peterfication commented 4 days ago

So the first problem is that I used a mapping of :Telescope Telescope neoclip<CR> which results in the problem of "No visual range allowed". If I map <CMD>Telescope neoclip<CR>, it works just fine.

However, the pasted text is inserted before of after the cursor, but it does not overwrite the visual selection. So I will look into the code to see whether this can be adapted in a reasonable amount of effort.

peterfication commented 4 days ago

According to this comment, it might not be possible in neoclip at all, but needs to be handled outside of neoclip.

The problem is that you loose the visual selection as soon as Telescope opens. So one solution could be to write a function that saves the visual selection somewhere and on select of a neoclip entry select the visual range again.

peterfication commented 3 days ago

So I made it work with the following:

Visual keymap: <ESC><CMD>Telescope neoclip<CR> (The <ESC> is necessary to "save" the visual selection).

Custom neoclip action:

      local function visual_paste(opts)
        local handlers = require("neoclip.handlers")
        handlers.set_registers({ 'z' }, opts.entry)
        vim.api.nvim_feedkeys('gv"zp', "n", false)
      end

      require("neoclip").setup({
        -- ...
        keys = {
          telescope = {
            n = {
              custom = {
                ["v"] = visual_paste
              },
            },
            i = {
              custom = {
                ["<c-v>"] = visual_paste
              },
            },
          },
        },
      })

@AckslD Should I contribute this to the README?