Tools of the trade: fzf

In the second post of this series, let’s talk about fzf.

What is fzf?#

In its simplest form, fzf is a fuzzy finder. It lets you search through files, folders, any line-based text using a simple fuzzy and/or regex backed system.

On-demand, fzf can also be super fancy.

Why do I use it?#

Because fzf is a search tool, you can use it to find files and folders. My most common use-case for it is a simple bash function that goes like this:

# find-and-open, gedit? Sorry I'll just stop.
function fao() {
  local ARG;
  ARG="${1}";
  if [ -z "${ARG}" ]; then
    nano "$(fzf)";
  else
    nano "$(fzf -q"${ARG}")";
  fi
}

It starts up a fzf session and then opens up the selected file in nano.

By default, fzf is a full-screen tool and takes up the entire height of your terminal. I’ve restricted it to 40% of that, as it looks a bit nicer IMO. You can make more such changes by setting the FZF_DEFAULT_OPTS environment variable as described in the layout section of the fzf docs.

But that’s not all! You can get real fancy with fzf.

For example, check out the output of fzf --preview 'bat --style=numbers --color=always --line-range :500 {}' here (a bit too wide to embed here :()

bat is a cat(1) clone with syntax highlighting and other nifty features, and also a tool I use on the daily. We’ll probably be covering it soon :)

You can also bind arbitrary keys to actions with relative ease.

The syntax as evident, is pretty simple

<key-shortcut>:execute(<command>)<+abort>

The +abort there is optional, and signals fzf that we want to exit after running the command. Detailed instructions are available in the fzf README.

And that’s it from me. Tweet at me at @MSF_Jarvis with any fancy fzf recipes you come up with!

This was part 2 of the Tools of the trade series.

You can sponsor me through GitHub to support this blog, my work on Password Store and all my other open source projects.