Hacker News new | ask | show | jobs
by mirashii 928 days ago
I'll throw in sd as a nice sed/find-and-replace tool. Using fd + xargs + sd is a pretty good workflow if a shell glob isn't good enough to target the files you want. https://github.com/chmln/sd
2 comments

I’ll also throw in Leah Neukirche ‘s xe as a better alternative to xargs: https://github.com/leahneukirchen/xe
I wanted to like sd but it doesn't support my main use case of recursive search/replace. Imagine if every time you wanted to grep some files you had to build a find -print0 | xargs | rg pipeline... it just takes me out of the flow too much. I'm glad people are posting other options here, I'm looking forward to trying them.

https://github.com/chmln/sd/issues/62

If you haven't discovered recursive path expansion with `**` yet, which is supported by a number of popular shells, including bash, it is about to improve your shell life.
Agreed, doing sd 'search' 'replace' **.py is common in my history. I only mention fd + xargs as a backup for when you need to do something that a simple shell expansion won't cover.

Also, for a "confirm your changes" type workflow, I like git add -p after running the sd command just to review all of the changes.

How does that work?
It's glob and/or bsd_glob from the underlying C stdlib. Another interesting example of things the stdlib glob can do:

  #!/usr/bin/perl
  # perl since it's an easy way to use an arbitrary function from stdlib
  use File::Glob "bsd_glob";
  for (bsd_glob("{Hello,Hi} {world,everyone}...a {demo,example}")) {
    print($_."\n");
  }