FZF: the best fuzzy finder for the terminal (In my opinion)

Note

This blog is written with the assumption that you use the fish shell, but if you don't use fish shell continue reading anyways. You might find something you like.


FZF

What is FZF? FZF is a terminal fuzzy finder created by junegunn. It can find files just by typing parts of the file name hence "fuzzy". You can learn more about it in it's github.

FZF without configs

FZF without configs is pretty bland, it looks bad, it's not the worst but it doesn't look the best. Screenshot from 2021-05-26 13-41-46.png

It doesn't look this bad, the reason the colors don't work well is because I have the nord theme configured for my terminal emulator.

Using FZF

You can find files with FZF, but it doesn't open the editor which was set in your $EDITOR variable. Instead it returns the path of that file which makes it very useful since you can pipe it into anything else that excepts strings as input.

For example, piping the output into vim/neovim/other terminal based text editor.

Try this:

  1. Figure out which file you want to open with that editor then go to another directory away from it.
  2. Run this command nvim (fzf), replace nvim with whatever your editor is.

This opens fzf, you find your file then nvim will open with that file.

Adding the olive oil

Usually when you use vim it's inside a project directory with other files you want to edit/view. So you might want to use a file browser such as netrw, or NERDTree or :e. But you see files in the directory in which you ran nvim (fzf).

As mentioned previously, fzf will echo the file path back. And we can use that. I have a fish function called v which goes in the home directory, then runs fzf there so I have access to all files in my home directory. Because I have everything there.

Does some string manipulation and processing cds into the directory where you file lives. Then opens it in neovim.

function v
    cd $HOME

    set file (fzf)
    set split (string split / $file)

    set chdir "$split[1..-2]"
    set dir (string replace -a ' ' / $chdir)

    cd $dir
    nvim.appimage "$HOME/$file"
end

Adding even more style

My fzf has file previews, and a theme. It looks like this:

Screenshot from 2021-05-26 13-57-52.png

This can be done easily simply by adding this in your config.fish

export FZF_DEFAULT_OPTS="
--preview 'bat --style numbers,changes --theme Nord --color=always  {}'
--border rounded
--color fg:#D8DEE9,bg:#2E3440,hl:#A3BE8C,fg+:#D8DEE9,bg+:#434C5E,hl+:#A3BE8C
--color pointer:#BF616A,info:#4C566A,spinner:#4C566A,header:#4C566A,prompt:#81A1C1,marker:#EBCB8B
"

The file preview is set with the --preview flag, and I use bat, as a replacement for cat because it is a superior alternative to cat.

--color is how the nord theme is set, and --border just sets the border style. I made it rounded. Refer to each tools man page for how to configure them to your liking.