5 ms·
rg is great, I use it a lot. Recently I have also used [ambr](https://github.com/dalance/amber https://github.com/dalance/amber) which can do both search (ambs)
by grudg3 3y ago
rg is great, I use it a lot.
Recently I have also used [ambr](https://github.com/dalance/amber https://github.com/dalance/amber) which can do both search (ambs) and replace (ambr) recursively in your codebase. The only problem as of yet is that it does not support globbing so I cannot filter on certain filetypes only.
- nextaccountic 3y agoSee also ast-grep https://github.com/ast-grep/ast-grep/ https://github.com/ast-grep/ast-grep/ > ast-grep is a AST-based tool to search code by pattern code. Think it as your old-friend grep but it matches AST nodes instead of text. You can write patterns as if you are writing ordinary code. It will match all code that has the same syntactical structure. You can use $ sign + upper case letters as wildcard, e.g. $MATCH, to match any single AST node. Think it as REGEX dot ., except it is not textual.
- coldtea 3y agoYou could use "find" (or even better, "fd") to find specific filetypes, then pass it to amber via xargs or some similar way.
- mirashii 3y agoI'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 https://github.com/chmln/sd
- cstrahan 3y agoI’ll also throw in Leah Neukirche ‘s xe as a better alternative to xargs: https://github.com/leahneukirchen/xe https://github.com/leahneukirchen/xe
- ForkMeOnTinder 3y agoI 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 https://github.com/chmln/sd/issues/62
- oftenwrong 3y agoIf 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.
- gitaarik 3y agoHow does that work?
- tyingq 3y agoIt'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"); }
- mirashii 3y agoAgreed, 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.
- nicoburns 3y agohttps://github.com/facebookincubator/fastmod https://github.com/facebookincubator/fastmod is also great for the replace usecase.
- wwarner 3y agoThis use case is a killer feature of emacs. Rg supports wgrep (writable grep). `rg` to match lines, and then in the resulting buffer, `e` to edit the results and save the changes back to the matched files.