Restricting ripgrep to certain file types
I’ve been using ripgrep to search files for awhile, but I hadn’t figured out how to recursively search only certain file types. Anytime I needed to search, say, for any occurrence of “doFoo” in Go source files, I’d type:
$ rg 'doFoo' *.go
*.go: No such file or directory (os error 2)
No files were searched, which means ripgrep probably applied a filter you didn't expect. Try running again with --debug.
$ find . -name '*.go' -type f -print0 | xargs -0 grep 'doFoo'
And then I resolve to get around to figuring out how to ripgrep through only certain file types.
Last week, I finally had had enough, and I started looking for answers. I googled “ripgrep path” and “ripgrep glob” and also tried typing things like “ripgrep ‘doFoo’ **/*.go”. I brought up ripgrep’s man page and searched for “path” and “glob.” I mean, I felt like I scoured that man page. I couldn’t find anything to help, though.
Finally, though, one of of my google searches unearthed this article: Turbo charging your command line with ripgrep. It contained info on the Type flag:
Search the contents of .html and .css files only for the word foobar using the Type flag (t).
$ rg -thtml -tcss foobar
Or return everything but css files using the Type Not flag (T):
$ rg -Tjs foobar
I looked back at the man page, and there it was, all along:
-t, --type TYPE ...
Only search files matching TYPE. Multiple type flags may be provided. Use the
--type-list flag to list all available types.
Knowing what to search for is half the battle. Thank you, Joseph Woodward!
Update (Aug 5, 2018): ripgrep’s author tweeted me a link to the relevant documentation. So really I had no excuse but sloth for my ignorance! You can find the full guide at:
https://github.com/BurntSushi/ripgrep/blob/master/GUIDE.md
I’m going to take the time to read the whole guide and find out what else I’m missing. Tomorrow.
Say what you want but I found this via google search before I found any of the resources you mentioned. ripgrep is loaded w/ features I still don’t know about but that’s okay. For instance a comment on the ripgrep github for ignoring certain directories was way more helpful than the documentation because it exposed a little glitch that occurs only in zsh so the creator was like “This isn’t a bug GTFO” and closed the request but then someone came in months later and answered the Q
Thanks for sharing. Some of us need documentation for the documentation!