ripgrep + fzf + vim = super vim
For a long time, I have been in search for perfect plugin/settings for fuzzy file finder for vim. I came across fzf and liked it instantaneously. Its fast, portable and easily integrates with vim.
The problem with fzf is that it doesn’t respect .gitignore files. There are lots of unnecessary entries in the interactive finder as explained here — FZF Respecting .gitignore. So today, we will try to solve this issue.
FZF is a general-purpose command-line fuzzy finder. Installing it on your system is very easy. You can use Homebrew
to install it on your Mac by simply executing brew install fzf
.
Detailed instruction for installing fzf for other platforms can be found here.
Fzf in action
I have added several other settings to my bashrc file to make it look like this.
export FZF_DEFAULT_OPTS='--height 40% --layout=reverse --border‘
Now the problem comes when we have lots of files in the project and we want to ignore the files which are not being tracked by our version control systems.
For the sake of explaining I will make another file in the project demo3.rb
and add this file to my .gitignore
file.
fzf doesn’t respect gitignore
As you can see in the above image, fzf shows demo3.rb
even if it's being ignored in git.
The solution to this problem is given here, according to which we will have to use another command instead of the default find
, to traverse the system. find
command does not respect .gitignore
files. Its just dummy finder utility program. We have several options for the program to use instead of find
.
I choose ripgrep, as explained in the comparison here it’s the fastest search tool available.
Installing ripgrep
is very simple. You can run brew install ripgrep
in your terminal to install it on your Mac. To install it on different platforms, you can follow the steps explained here.
After installing it we need to set FZF_DEFAULT_COMMAND
in your bashrc
file like this.
export FZF_DEFAULT_COMMAND='rg --files'
--files
means
Print the files that ripgrep would search, but don’t actually search them.
You can take look at all the option that can be accepted by ripgrep here.
And the result looks like this
fzf respecting gitignore
Yay!!! 🎉
With bigger projects, comes another problem. What if we have another file which is in gitignore but we still want to search the file…
For eg. in Rails, we have application.yml
file, which we don’t usually push to our git repository but we still want to find that via fzf.
For the sake of simplicity, I will make another file demo4.rb
and add it to .gitignore
file.
but I want to see demo4.rb in file finder…
As you can see in the above image, we now have 4 files. Both demo3.rb
and demo4.rb
files are in .gitignore
. And fzf
totally ignores both the files. 😞
If we see the documentation of Ripgrep we see an option which lets us ignore .gitignore in the searching files viz.--no-ignore
.
But that would mean all the things we did earlier was a waste of time.
Ah, what to do now? 😠
Turns out we can make a file .rgignore
in our project which lets us solve this issue. It says in official documentation of Ripgrep —
Sometimes you want to search files that are in your
.gitignore
, so it is possible to specify additional ignore rules or overrides in a.ignore
(application-agnostic) or.rgignore
(ripgrep specific) file.
I made .rgignore
file in the project root folder and added following to it
!demo4.rb
which essentially means that we don’t want to ignore demo4.rb
file from the search. It overrides our .gitignore
file and add back the demo4.rb
file in the list of files to be searched. And the result looks like this
whoo hoo !!! 🎉
Now integrating all this with vim is very easy. You can install fzf.vim plugin and use it in vim.
Got questions? Just ask.