chrisgrieser / nvim-rip-substitute

Perform search and replace operations in the current buffer using a modern user interface and contemporary regex syntax.
MIT License
127 stars 7 forks source link

Feature Request: replace with empty string (basically delete a match) #13

Closed farzadmf closed 4 weeks ago

farzadmf commented 4 weeks ago

Checklist

Feature Requested

Hey there, I have a use case to find a pattern and delete it basically (something like %s/blah-//).

I tried to play with the extension and researched a bit, but I'm not sure how can I specify "empty string" as the replacement.

Sorry if this is not a feature request; maybe more of a question

Relevant Screenshot

No response

chrisgrieser commented 4 weeks ago

Just leave the second line (the replace line) empty:

Pasted image 2024-07-08 at 18 44 08@2x

if you confirm now, it replaces everything with an empty string.

farzadmf commented 4 weeks ago

Oh cool, thank you for the quick answer @chrisgrieser !

Is there a way to also reflect it in the preview? Since it wasn't reflected in the preview, I was under the impression that it won't do anything (and clearly I was wrong)

chrisgrieser commented 4 weeks ago

Oh I see. Yeah, that one is a bit of a dilemma. Basically, there are two kinds of previews: a) highlight the search matches, or b) show the planned replacement.

:substitute changes the way it does incremental previews depending on the number of slashes: % s/bla highlights all bla in the buffer (= a), % s/bla/ removes all bla in the buffer, since it's assuming an empty replacement string (= b). Basically, the number of slashes determines when the preview should switch from a) to b).

However, since rip-substitute does not use any delimiters, the plugin uses the type a) preview as long as the replacement line is empty, and the type b) preview as soon as you type something into the replacement line. This works fine basically the same as with :substitute, but it appears preview with an empty string as replacement is exactly the one edge case where it differs.

To be honest, I have no idea how to better deal with the "empty string replacement" scenario. If you got an idea, I'm all ears. Otherwise, I'm afraid, this is simply the one edge case we have to live with, basically the "price" of dropping the need for delimiters I guess. 🤔

farzadmf commented 4 weeks ago

basically the "price" of dropping the need for delimiters I guess. 🤔

Totally understandable; I guess there should be compromises, and also the fact that maybe removing things is not a very common use case.

One "hack" I can think of (really a [terrible] hack that I just thought of as I was writing this), is to maybe have a "special character" to mean "empty" and handle that separately before passing things to ripgrep.

As I said, a super quick hack that came to my mind, so you can forget about it 😆