"Mo"
Published on

ripgrep and the rgignore file

Authors

I’m a huge fan of ripgrep. And that’s not an exaggeration. It’s literally my number one used tool on the terminal.

Terminal window
$ history --search | string trim | string replace -r ' .*' '' | sort | uniq -c | sort -nr | head -n 10
2913 rg
1005 git
369 cd
322 ls
287 cat
285 rm
226 fd
188 brew
168 j
162 gh

So, to learn something new about it makes for a special occasion.

ripgrep already has built in support for ignoring files based on your .gitignore file. I’ve known this for a while. And I’ve read this line in the README before, too…

Namely, ripgrep won’t search files ignored by your .gitignore/.ignore/.rgignore files, it won’t search hidden files and it won’t search binary files. Automatic filtering can be disabled with rg -uuu.

So, I knew ripgrep had its own ignore file. But I never knew why I might care. It felt weird. Then I realized why.

Check out this snippet:

Terminal window
$ rg FirstName
src/lib/Foo.Lib/Data/Migrations/20251001172535_SomeRandomEnhancements.Designer.cs
2850: b.Property<string>("FirstName")
4295: b.Property<string>("FirstName")
5326: b.Property<string>("FatherFirstName")
5349: b.Property<string>("FirstName")
5418: b.Property<string>("MotherFirstName")
5555: b.Property<string>("SpouseFirstName")
5690: b.Property<string>("FirstName")
8135: b.Property<string>("FirstName")
12923: b.Property<string>("FirstName")
src/lib/Foo.Lib/Models/PersonModel.cs
8: public string FirstName { get; set; } = string.Empty;
src/lib/Foo.Lib/Data/Migrations/20251106142304_OtherMigration.Designer.cs
2853: b.Property<string>("FirstName")
4298: b.Property<string>("FirstName")
5329: b.Property<string>("FatherFirstName")
5352: b.Property<string>("FirstName")
5421: b.Property<string>("MotherFirstName")
5558: b.Property<string>("SpouseFirstName")
5693: b.Property<string>("FirstName")
8146: b.Property<string>("FirstName")
12960: b.Property<string>("FirstName")

The migration designer file is pure noise when I’m looking for usage. Multiply this times every migration file in the source. Or, if you still have a system with minified javascript in source (not every project has switched to package.json, still)? Even worse.

If I add a new .rgignore file, though, that has this line:

src/lib/Foo.Lib/Data/Migrations/*.Designer.cs
*.min.js
*.min.css
*.js.map

It now will only show the match I care about:

Terminal window
$ rg FirstName
src/lib/Foo.Lib/Models/PersonModel.cs
8: public string FirstName { get; set; } = string.Empty;

Disclaimer - there is a risk that you’ll miss matches with this. But that’s why ripgrep has support to disable the filters, too.

Hope this helps someone out there.