Filter ripgrep's Results When You Don't Have a Git Repo
This ignore file based approach works both on the command line and when searching files in Vim, such as with FZF.
Prefer video? Here it is on YouTube.
If you have a git repo then ripgrep
will automatically use your .gitignore
file to skip or filter results based
on that. That’s very handy to remove unwanted results.
What if your project isn’t in git but you still want to create project specific ignores?
You have 2 options to apply this to a specific project:
- Create an
.ignore
file which uses similar syntax as a.gitignore
file - Create a
.gitignore
file and an empty.git/
directory (or file)
I’d suggest option 1 but option 2 is kind of interesting. It’s what I tried
first but noticed ripgrep doesn’t read the .gitignore
file unless there’s a
.git/
path present. Ultimately I went with option 1 after discovering this
issue on GitHub.
Use case
I recently switched my entire site from Jekyll to Hugo and while going through that process I wanted to improve my experience when writing and publishing blog posts. It was time for a fresh outlook on everything.
I don’t have my site in git because there’s 1.4 GB of images.
In the old Jekyll set up I did configure ripgrep to at least ignore files in
the _sites
directory which is all of the static file output but that was done
in ripgrep’s global settings.
However, it still returned results for images.
I use CTRL+p
to fuzzy find files with FZF and when you have 500+ posts and 300+ drafts with images that
have the same title such as hello-world.jpg
with a post of hello-world
that
creates a lot of extra noise in FZF’s results. 100% of the time I want to open
the blog post not the image so we might as well filter out images.
That’s when I looked around for a solution and found this. For Hugo, my
.ignore
file looks like this. Keep in mind I customized my Hugo assetDir
and publishDir
directories which is what public/
and published/
are:
public/*
!public/.keep
published/
.env*
!.env.example
.hugo_build_lock
*.jpg
*.png
*.gif
The demo video below shows how it works.
# Demo Video
Timestamps
- 0:26 – Fuzzy matching files with FZF inside of Vim
- 0:49 – Using an .ignore file to filter matches
- 1:05 – A before and after without filtering
- 1:57 – Technically you can make a dummy .git directory and .gitignore file too
- 2:28 – Sometimes change can introduce nice improvements
Are you going to try this out? Let me know below.