Skip to content

Instantly share code, notes, and snippets.

@bag-man
Created February 4, 2017 00:01
Show Gist options
  • Save bag-man/076433a0fc0da1b8f382c80b9697f823 to your computer and use it in GitHub Desktop.
Save bag-man/076433a0fc0da1b8f382c80b9697f823 to your computer and use it in GitHub Desktop.
fzf + rgrep + vim mini tutorial

I've always had fzf and ripgrep on my radar, and I've finally gotten around to using them together. Good lord it makes a world of difference, especially when added to Vim as well as Bash.

Add the following snippet to your ~/.bashrc, this add's fzf keybindings to bash and gets fzf to use ripgrep by default for faster searching.

[ -f ~/.fzf.bash ] && source ~/.fzf.bash
export FZF_DEFAULT_COMMAND='rg --files --no-ignore --hidden --follow --glob "!.git/*"'
bind -x '"\C-p": vim $(fzf);'

Okay now what can you do?

  • Ctrl + r - search through bash history with fzf
  • Ctrl + p - edit a file in vim from fzf
  • mv dir/** - expand a directory with (**) and select from fzf
  • Alt + c - change directory from fzf
  • Ctrl + t - insert file from fzf into command

Neat right! Now if you are a vim user there is more, add the fzf plugin to your ~/.vimrc, along with this snippet. Obviously customise the bindings, and excludes / includes to your workflows!

let g:rg_command = '
  \ rg --column --line-number --no-heading --fixed-strings --ignore-case --no-ignore --hidden --follow --color "always"
  \ -g "*.{js,json,php,md,styl,jade,html,config,py,cpp,c,go,hs,rb,conf}"
  \ -g "!*.{min.js,swp,o,zip}" 
  \ -g "!{.git,node_modules,vendor}/*" '

command! -bang -nargs=* F call fzf#vim#grep(g:rg_command .shellescape(<q-args>), 1, <bang>0)

You now have a killer free text search with :F that uses ripgrep and is faster than any I've seen before.

I've done more with this, but want to leave it there for now. There are even more goodies in my .vimrc, and in my .bashrc, including auto installing fzf and ripgrep (admittedly hackily) from the .vimrc and a nice snippet that uses fzf for git logs.

@RidaAyed
Copy link

Hi

Awesome article, thanks so much. Any idea where I should start digging in order to get this working?

mv dir/** - expand a directory with (**) and select from fzf

On tabbing I receive the arbitrary linux message Do you want to expand all n possibilities?

Everything else works like a charm 🚀

@zenVentzi
Copy link

I couldn't find the .fzf.bash file in your repo that(I guess) is sourcing the bindings. With only the code above, C-p works but not the others.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment