DEV Community

Cover image for How to Use Fzf with Ripgrep to Selectively Ignore VCS Files
Mohammed Boudad
Mohammed Boudad

Posted on

How to Use Fzf with Ripgrep to Selectively Ignore VCS Files

As developers, most of the time we want to make sure not to commit some directories (e.g: node_modules) and files (e.g: .env) to a certain repository, so we put them in .gitignore. Now, the problem you may face is when you want to make a quick search for certain files under your working directory using a tool such as fzf you don't want the search result to get cluttered up with files from node_modules which you rarely want to take a look at, but still you want to keep files like .env in the search result and that's what we're going to do using fzf and ripgrep.

Installation

If you have fzf, fzf.vim and ripgrep already installed in your machine then skip to Configuration, otherwise follow the steps below:

  1. To install fzf you can just clone the repository and run the install script:
git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf
~/.fzf/install
Enter fullscreen mode Exit fullscreen mode
  1. To install ripgrep use your system's package manager, in my case:
sudo apt-get install ripgrep 
Enter fullscreen mode Exit fullscreen mode

Note: the binary name for ripgrep is rg, that means if you want to check if the installation is successful you can just run rg --version (ripgrep won't work).

If you want fzf to work on vim, add the following two lines using vim-plug to your ~/.vimrc:

Plug '~/.fzf'
Plug 'junegunn/fzf.vim'
Enter fullscreen mode Exit fullscreen mode

Configuration

We are going to add some commands to ~/.zshrc (or ~/.bashrc depending on your preferred shell). First add these two lines:

export FZF_DEFAULT_COMMAND='rg --files --hidden --follow --no-ignore-vcs'
export FZF_CTRL_T_COMMAND="$FZF_DEFAULT_COMMAND"
Enter fullscreen mode Exit fullscreen mode

In the lines above we set rg as the source for fzf. We add --files option to print the files that ripgrep would search, followed with:

  • --hidden: to include hidden files
  • --follow: to include symbolic links
  • And finally --no-ingore-vcs to tell it to not ignore version control files

What all these basically do, is tell ripgrep to include all the files in the search.

You can also add some FZF_DEFAULT_OPTS if you like to customize the look of fzf to your preference. These are the options I use:

export FZF_DEFAULT_OPTS='--height 96% --reverse --preview "cat {}"'
Enter fullscreen mode Exit fullscreen mode

In the above command we give fzf some default options:

  • --height 96%: to make fzf take up 96% of the terminal window
  • --reverse: to make fzf display in reverse
  • --preview "cat {}": to get a sneak peak of the selected file's content

Now let's create an alias, which we will give it the name fzfi:

alias fzfi='rg --files --hidden --follow --no-ignore-vcs -g "!{node_modules,.git}" | fzf`
Enter fullscreen mode Exit fullscreen mode

In the above command we tell ripgrep to exclude node_modules and .git directories from the search and keep the other files.

Now with this, we made fzf command by default search all files in our working directory, and fzfi quickly search all the files excluding node_modules and .git directories, so files which you don't like to commit to your repository, but still like to read or edit such as .env are included in the search result.

Using the above alias, you might as well want to quickly search and open a file with vim, by adding this alias:

alias vifi='vim $(fzfi)'
Enter fullscreen mode Exit fullscreen mode

This is all regarding making the search from the command line. Now when we want to make a quick search from vim with the same options as the alias vifi, we need to create a new command in ~/.vimrc, and we are going to name it All:

command! -bang -nargs=*  All
  \ call fzf#run(fzf#wrap({'source': 'rg --files --hidden --no-ignore-vcs --glob "!{node_modules/*,.git/*}"', 'down': '40%', 'options': '--expect=ctrl-t,ctrl-x,ctrl-v --multi --reverse' }))

Enter fullscreen mode Exit fullscreen mode

With this we can map our new command in normal mode by adding:

nnoremap <silent> <leader>o :All<cr>
Enter fullscreen mode Exit fullscreen mode

This is how it will look like in vim:

fzf in a horizontal split

I hope this article was helpful. Happy coding!

Top comments (3)

Collapse
 
dietrich profile image
Dietrich Moerman

Great article!

In addition to passing ripgrep options through FZF_DEFAULT_COMMAND, you can as well set the environment variable RIPGREP_CONFIG_PATH to a config file listing the options to apply to ripgrep by default. Over there I set:

--no-ignore-vcs
--hidden
Enter fullscreen mode Exit fullscreen mode

What's even more convenient, though, is to have an ~/.ignore file where you can list directories and files ripgrep should still ignore (but doesn't since we use --no-ignore-vcs):

.git/
node_modules/
Enter fullscreen mode Exit fullscreen mode

This avoids having to define an additional :All vim command and mapping.

Dietrich

Collapse
 
matrixersp profile image
Mohammed Boudad

Thank you!

What's even more convenient, though, is to have an ~/.ignore file where you can list directories and files ripgrep should still ignore (but doesn't since we use --no-ignore-vcs):

I tried with ~/.ignore file but it doesn't work unless created in the working directory, that's why I chose to do it this way.

Collapse
 
booterror profile image
Vighnesh SK

You can use git ls-files to list the files in the repo. The files ignored by .gitignore won't show up.