gbprod / yanky.nvim

Improved Yank and Put functionalities for Neovim
Do What The F*ck You Want To Public License
802 stars 22 forks source link

[Enhancement] Ignore Replaced Text #155

Closed tummetott closed 8 months ago

tummetott commented 8 months ago

It would be valuable to introduce an equivalent option in Yanky for the following keymap:

vim.keymap.set('x', 'p', '"_dP', { desc = 'Paste without copying replaced text' })

This option would ensure that when text is selected and pasted, the replaced text is not added to the " register (or in our case the yank ring).

The rationale behind this is that in most cases when selecting and replacing text with the contents of the " register, there is no desire to retain the replaced text. Often, this operation is repeated in different parts of the file, and preventing the replaced text from accumulating in the yank ring would be beneficial.

The option could be named as opts.ring.ignore_replaced_text

adamsitar commented 8 months ago

Yes please!

Meanwhile, @tummetott, have you found a workaround?

gbprod commented 8 months ago

Hey!

I think you can just do : vim.keymap.set('x', 'p', '"_<Plug>(YankyPutAfter)', { desc = 'Paste without copying replaced text' }) Another solution (mine) is to use substitute.nvim instead (see also https://github.com/gbprod/yanky.nvim#-integrations)

Anyway, I will think about add an option, but I'm not convinced for the moment.

tummetott commented 8 months ago

vim.keymap.set('x', 'p', '"_<Plug>(YankyPutAfter)', { desc = 'Paste without copying replaced text' })

This does not work unfortunately 🙁

gbprod commented 8 months ago

Oh! Sorry, you're right, that does not works... I try to find another way.

gbprod commented 8 months ago

vim.keymap.set('x', 'p', '"_d<Plug>(YankyPutAfter)', { desc = 'Paste without copying replaced text' }) does this works for you ? (otherwise I recommend substitute.nvim ;) )

tummetott commented 8 months ago

vim.keymap.set('x', 'p', '"_d<Plug>(YankyPutBefore)', { desc = 'Paste without copying replaced text' }) would be the command (note Before not After)

However, this introduces an issue: after replacing highlighted text and subsequently pressing <c-n> (or <c-p>), the replaced text reappears, positioned immediately following the next item in the yankring. I suspect the plugin initiates an undo action, followed by pasting the next yankring item. Regrettably, this process also reverses the effect of the "_d portion of the keymap. In my opinion, it's crucial to include the "_d action within the plugin to address this issue effectively.

Using substitute.nvim is not an option for me. I've previously experimented with this plugin, but it deviates significantly from the traditional Vim usage that I'm more comfortable with. I prefer to work with native Vim commands with slight custom enhancements.

tummetott commented 8 months ago

After conducting some testing, I discovered that <Plug>(YankyPutBefore) already achieves precisely what I want when executed as a visual mode command. I had initially assumed that YankyPutAfter and YankyPutBefore would behave similarly when executed on a visual selection, as there is no clear "after" or "before" concept in this context; they both place the text at the selection's position. However, it turns out that the former adds the replaced text to the yankring, while the latter does not. I'm unsure whether this was the intended behavior, but I've successfully remapped it using the following command, and it works as expected. 😄

vim.keymap.set('x', 'p', '<Plug>(YankyPutBefore)', { desc = 'Paste without copying replaced text' })