Rep and Ren: A new approach to CLI find, replace, and renaming
blog.robenkleene.com
blog.robenkleene.com
The rep approach to use the actual ripgrep output seems very clever, tho I wonder how well it works in practice, but I'll be sure to give it a try.
https://gist.github.com/jaysoffian/0eda35a6a41f500ba5c458f02...
Of course this also repeats the pattern, but that's an implementation detail I let the script handle for me. A future improvement to `rep` could be for it to invoke `rg` directly in order to avoid needing to repeat the pattern, though I suppose that could be a shell script easily enough.
[1]: How often are folks doing a global search and replace on a mass of files that isn't checked-in to a repo? I've been a sys admin and programmer for 25+ years and I think I can count on one hand the times I've ever needed to do that. If I had to do it today, I'd very likely create a temporary repo first in order to have a quick way to verify the diff and restore to previous state if need be.
git ls-files **/*.lua | sad foo barAs a vim user, I have never really gotten into the quick fix list. I want to do my stuff in the command line.
But I find that a lot of new command line tools don't match my thinking, so I struggle to use them. (One example is fzf, which it seems people love.)
I do love ripgrep, though. I also love tools where I can see what action it performs before actually doing it. I use `-n` on ZFS all the time.
But making the action itself require a flag? Brilliant. Using stsndard streams? Brilliant. Showing what it will do, not the end result? Brilliant.
I'm going to download and and start experimenting right away.
$ rg --passthru search -r replace file.txt | sponge file.txt
And preview changes like this: $ rg --passthru search -r replace file.txt | diff -u file.txt -
Of course, this is only practical when working with a single file. Though I'd imagine if one threw in the `--files-with-matches -0` flags together and piped that into `xargs`, something similar to `rep` could be be achieved. (Not that I'd encourage anyone to do this at all)Why so?
I just personally would feel more confident with using perl for anything to do with multi-file substitutions. In theory, ripgrep with `--passthru` and `-r` should work.
If I have time, I'll check these utils to see if they handle all pathnames correctly.
reminds me of emacs wdired mode
Particularly as we get more investment in AI driving tooling, this sort of lift might be more and more common.
Wonder how well it compares with fastmod [0]? That's what I've been using for large scale codemods/refactors. ripgrep is ofc insanely fast so ripgrep+ren would probably fare favorably.
Edit: I've now tried it myself with `rep`.
It's not automatic, unfortunately. You can use `vipe` (part of moreutils on my machine) to pipe into vim (or any `$EDITOR`) and to pipe the result out.
However, `rep` wants a pager. If you run `vim` with `rep` at the end of the pipeline, everything gets garbled because `rep` (well, the pager) is the one taking key presses.
To get around this, you can pipe `rep` to something else. I piped it to `cat -`, which forced it into outputting to `stdout`.
So my final command looked like this:
rg --no-heading --with-filename --line-number "\bstatic inline\b" include/ src/ tests/ \
| vipe | rep inline y_cinline | cat -
Make sure your `$EDITOR` variable is set correctly for `vipe`.Anyway, I'm going to add a PR to `rep` to add a command-line flag to output to stdout without a pipe. If it is accepted, that should help.
Personally, I prefer git grep