Following on from my articles on reflection time and combining rg & fzf I wanted to expand on one of my more powerful use cases for these great tools that can have the biggest impact on reflection time.

What I did was make a nice little bash function that uses, rg to search for strings in text files, fzf to display, search and filter the results, and then plumb the results into vim. This lets you find and edit files really rather fast!

sf example

This GIF is not in anyway representative of the smoothness of fzf!

What do you need?

Well first of all you are going to need to install fzf, ripgrep and then ideally you need to have an editor that supports opening files that have line and column numbers included. Then add this function to your ~/.bashrc, and you probably also want to edit the includes and excludes too.

sf() {
  if [ "$#" -lt 1 ]; then echo "Supply string to search for!"; return 1; fi
  printf -v search "%q" "$*"
  rg_command='rg --column --line-number --no-heading --fixed-strings --ignore-case --no-ignore --hidden --follow --color "always" -g "*.{'$include'}" -g "!{'$exclude'}/*"'
  files=`eval $rg_command $search | fzf --ansi --multi --reverse | awk -F ':' '{print $1":"$2":"$3}'`
  [[ -n "$files" ]] && ${EDITOR:-vim} $files

If your editor doesn't support opening with the location, you can still use this, you just need to edit the awk function from print $1":"$2":"$3 to simply print $1. However I would highly recommend trying to find a way for your editor to support it. With vim you just the vim-fetch plugin and you should be set.

What can it do?

This is great for jumping into a codebase at the exact point you want, and with the power of fzf's filtering it is really easy to narrow down a search and get to the right point in the code as quickly as you can. This is demonstrated in the GIF above.

You can also use <tab> to select and open multiple files, then you are a vim user and want to do a replace, you can quite easily run a replace across all the files with :bufdo %s/foo/bar/ge | update. Of if your regex-fu isn't up to scratch, just make your changes, save them, and then close each buffer with :bd.


If you like this and want more check out my original article on fzf & rg, or have a look at my bashrc to see the other cool tricks I'm using. There is also a huge number of other great examples on the fzf wiki.


BTC | 15FYeMgQsETViUcc8JtSmoHmCAmADy2Ymn
ARK | ANKmYTHV7QdkcLz2R65uMzEvcRRdz2iRQm