1. 40
  1. 13

    I’m a daily user of ripgrep, but my use is limited to rg pattern -g '*.ext' directory.

    There is a lot of cool stuff in this article.

    1. 7

      Thanks.

      I have a chapter on ripgrep (https://learnbyexample.github.io/learn_gnugrep_ripgrep/ripgrep.html) if you are interested in learning about various other options, Rust regex, etc.

    2. 11

      Pretty cool, thanks! I didn’t know ripgrep could do replacements.

      In-place editing can be done with the sponge utility from moreutils:

      rg --passthru 'blue' -r 'red' ip.txt | sponge ip.txt
      
      1. 1

        You’re welcome.

        sponge is a nice suggestion, I’ll add it to the post, thanks.

      2. 4

        alias hl='rg -N --passthru' makes a nice highlighter

          1. 2

            I use the older codemod tool regularly – didn’t know that a faster Rust version existed. Thanks!

            1. 1

              Is there a specific way to integrate rg with fastmod?

              1. 3

                I don’t think so, but fastmod uses the same regex engine as ripgrep, although it probably doesn’t have all the optimizations that ripgrep has. (Specifically, inner literal optimizations. Those can’t really be added to the regex engine, so they live a layer above it inside of ripgrep, well, the grep-regex crate.)

            2. 2

              This looks quite nice - but I was disappointed to see that fixed string mode doesn’t extend to the replacement string, so you still have to escape stuff.

              Seems to defeat the purpose somewhat.

              1. 1

                Yeah, but it has a use if you need to use $0 as backreference. Also, search string starting with - requires special attention due to conflict with option parsing.

                $ echo 'a.b*{2}c' | rg -F '.b*{2}' -r ' $0 '
                a .b*{2} c
                
                $ echo 'a-b*c' | rg -r '+' -F -- '-b*'
                a+c
                

                You could check out sd, which supports String-literal mode (although I haven’t tested if it applies to replacement section as well)

                1. 2

                  Handling arguments starting with a hyphen/dash is a common thing (as you show, this is what -- is for), but the lack of “replace exactly this string with exactly this string” is surprising, in a tool that specifically has a “look for exactly this string” and “replace stuff in a string” functionalities.

                  ripgrep might have made it into tools I use from shell scripts, because it’s available from Debian buster onwards, but if its something I’m gonna need to compile and install on any target machines, I’m just as likely to just write a simple implementation myself in some higher level scripting language.

                  People by and large use grep and sed because they’re essentially ubiquitous - disregarding the various ‘flavours’, for the basic usage, they’re both part of the POSIX spec. Yes they probably have a speed advantage over a scripting languages for complex pattern based replacement, but unless you’re dealing with huge data or highly time sensitive processes, a lot of developers would be better served by writing a simple script in .

                  1. 2

                    The only thing you have to escape in the replacement string is the dollar sign: $$0 is $0. It’s simple enough IMO that a whole new flag for it seems like overkill to me.

                    And ripgrep isn’t designed to be a better sed. It has a very simple replacement feature and that’s it. It turns out you can get a lot of mileage out if it though.

              2. 2

                Great stuff. I didn’t know of search/replace. Does anyone here know if there is a flag or combination of tools so I can get truncated matches? i.e. so that the match is:

                $ rg dep
                README.md:
                7: ... Kubernetes *dep* loyment ...
                

                rather than the full line? I do want some context but if I searched a minified file I don’t want the whole line.

                1. 3

                  I think the solution you got in the replies is the best work-around for now, but there is an open feature request for this: https://github.com/BurntSushi/ripgrep/issues/1352

                  1. 1

                    Cool. Thank you.

                  2. 3

                    Printing only the matching part is -o, the same flag grep uses for this.

                    I always use -M 240 to cut off very long lines

                    You could rg -o '.{0,40}pattern.{0,40}' maybe?

                    Caveat: I only know what like 4% of rg’s flags do so maybe there’s a more direct way

                    1. 3

                      Clever trick, matching some amount before and after and then chopping with M. I like it. Cool. Let me see if I can just edit this in myself. I, too, know very few of the flags (and there are so many!).

                      1. 3

                        Thank you! The -M thing was a separate thought, I just have that there with an alias because super long lines mess up my workflow

                    2. 2

                      That’s a usecase I haven’t come across before, but I can see why it would be helpful with minified and other such lengthy inputs. The rg -o '.{0,40}pattern.{0,40}' suggestion in another comment seems the best way for this.

                      If you are on github, you could also ask the community: https://github.com/BurntSushi/ripgrep/discussions