BurntSushi / ripgrep

ripgrep recursively searches directories for a regex pattern while respecting your gitignore
The Unlicense
48.04k stars 1.98k forks source link

Include/exclude searched files with implicit wildcard or regex (ag -G) #2314

Open oandrew opened 2 years ago

oandrew commented 2 years ago

Describe your feature request

First, thanks for ripgrep.

There is one feature in silversearcher (ag) that's holding me back - filter file paths with regex e.g. ag <pattern> -G <regex>. ripgrep seems to support only globs (I'm guessing for performance reasons?)

99% of the time I simply use it to do a partial match, so having something like rg -G test func as shortcut to rg -g '*test*' func would totally resolve it for me.

So this boils down to:

  1. (simple) Could we have a new flag -G <glob> that's similar to -g <glob> but <glob> is implicitly surrounded with wildcards? Then you could avoid typing and escaping * in most cases. e.g. rg -G test func would be equivalent to rg -g '*test*' func
  2. (complex) rg -G <pattern> could accept a regex which would allow other use cases in addition to partial match.

Would you consider a PR for the first approach?

Thanks!

BurntSushi commented 2 years ago

It would be good to search the tracker first... :-) Dupe of #48, #54, #75, #91 (lots of discussion here), #193, #285.

There's just so many different ways to already do this that I don't see the point of adding a new flag for it. rg --files | rg test can be easily put into an alias or a wrapper shell script. Here's mine:

https://github.com/BurntSushi/dotfiles/blob/2f58eedf3b7f7dae7f0a7cea1a641459e25e5d07/.aliasrc#L111-L121

You could also use a purpose driven tool specifically for searching by file name: https://github.com/sharkdp/fd/

oandrew commented 2 years ago

@BurntSushi I wasn't referring to file search, but filtering files included in result (rg --glob). I searched the tracker but didn't find anything similar.

Updated the title to be less confusing

BurntSushi commented 2 years ago

Ooooo, I see. Sorry, I missed that.

I'll re-open this for now. I don't have time to respond, but I'm not quite inclined to accept this, at least as of now. ripgrep needs its filtering internals (and interface) overhauled at some point, so I'd rather not add anything substantially new to it as of now.

relicode commented 11 months ago

Minimalistic POSIX-compatible script with wildcard support:

#!/bin/sh

PATTERN="$1"
shift

(for DIR in "${@:-.}"; do rg --files "$DIR"; done) | rg "$PATTERN"