Supercharge Vim with Fzf and Ripgrep
Looking for something a little more light weight than Ctrlp or Unite or just wanting to refresh your existing fzf setup, well then this guide is for you.
For those of you that don’t know fzf is a command line fuzzy finder, it is insanely fast and super easy to extend.
ripgrep is a new tool similar to ack and ag but even faster and ships with some really useful additional parameters out of the box.
There are a number of ways to install ripgrep I myself installed rust and then used cargo: cargo install ripgrep
To kick things off first pick a decent plugin manager (if you haven’t already) I use vim-plug. Now add the following to your .vimrc
(Adjusting where necessary if you use an alternative plugin manager):
call plug#begin('~/.vim/plugged')Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': './install --all' }
Plug 'junegunn/fzf.vim'call plug#end(
Now run :PlugInstall
and this should install fzf
and fzf.vim
.
If your plugin manager can’t handle the install of fzf
follow instructions available on the project repo.
Next up add the following lines to you .bashrc
or .zshrc
:
# --files: List files that would be searched but do not search
# --no-ignore: Do not respect .gitignore, etc...
# --hidden: Search hidden files and folders
# --follow: Follow symlinks
# --glob: Additional conditions for search (in this case ignore everything in the .git/ folder)export FZF_DEFAULT_COMMAND='rg --files --no-ignore --hidden --follow --glob "!.git/*"'
The above line set the FZF_DEFAULT_COMMAND
when using :Files
in vim. If you are interested in what more you can do with rg
please use the help
flag.
To use your new fuzzy finder open up vim and run: :Files
this will list all of the files in your current project using rg
and even allow you to search the stream before it is complete because of fzf
and its awesomeness.
Now we are going to define our own :Find
command so we can leverage rg
for more than just finding files. To do this add the following to your .vimrc
:
" --column: Show column number
" --line-number: Show line number
" --no-heading: Do not show file headings in results
" --fixed-strings: Search term as a literal string
" --ignore-case: Case insensitive search
" --no-ignore: Do not respect .gitignore, etc...
" --hidden: Search hidden files and folders
" --follow: Follow symlinks
" --glob: Additional conditions for search (in this case ignore everything in the .git/ folder)
" --color: Search color optionscommand! -bang -nargs=* Find call fzf#vim#grep('rg --column --line-number --no-heading --fixed-strings --ignore-case --no-ignore --hidden --follow --glob "!.git/*" --color "always" '.shellescape(<q-args>), 1, <bang>0)
Now open up vim again and run :Find term
where term
is the string you want to search, this will open up a window similar to :Files
but will only list files that contain the term
searched.
The above command can result in some weird behaviour when used with vim in tmux (this does not apply to Neovim) where trailing characters may be present in the results, to fix this update your command as below:
command! -bang -nargs=* Find call fzf#vim#grep('rg --column --line-number --no-heading --fixed-strings --ignore-case --no-ignore --hidden --follow --glob "!.git/*" --color "always" '.shellescape(<q-args>).'| tr -d "\017"', 1, <bang>0)
As noted by reddit user Fwippy ripgrep can also be used for grepprg by adding the following block:
set grepprg=rg\ --vimgrep
And that’s it, any comments / suggestions are welcome, ripgrep is very new technology so the best way to handle this may soon change but I will try to keep this article as up-to-date as possible.