Skip to content

Stuff that you need: Vim + fzf

Files
“Files” by Indi Samarajiva (CC BY 2.0)

I would like to share a quick tip which can highly improve your workflow (at least it did with mine). If you usually work with the command line, you might have experienced that it is sometimes difficult to remember where is a certain file located. You might have some idea of which directory it was it in, but you might not even remember exactly the file name. For sure, tools like find can help you, but they are not as fast and convenient to do as one might like.

If you have ever felt like this, you need fzf.

As its Github page states, fzf is a general-purpose command-line fuzzy finder. This means that it can find strings in a glimpse, even if you do not write the string correctly. For example, I want to view a file with a name that is somehow similar to “GreatLinuxApplication.c”. Or was it “great_linux_application.c”? With fzf, you do not need to worry, since if you write something at least similar to the real application name, it will find it for you.

fzf can be used in many ways, although three key tasks that I very easily incorporated into my workflow are:

  1. Use fzf to navigate the command history (Ctrl+r).
  2. Use fzf to search for files at uncertain locations (Ctrl+t).
  3. Use fzf inside vim to open new tabs.

Key bindings for 1 and 2 are provided by fzf during installation (although they are optional). The following screenshots depict both methods.

Overwrite default Ctrl+r keybinding so that fzf is used to search the command history
Look for file names using fuzzy search. You do not need to know the full path to the file anymore.

With regard to vim integration, it does not come out of the box, but it is very easy to set up. You simply need to follow the instructions to install this plugin. Once installed, I added a key binding to launch fzf from inside vim.

noremap <C-f> :FZF <CR>

My workflow then becomes:

  1. Open vim without arguments.
  2. Hit Ctrl+f to launch fzf  at. Then I write the name of the file that I want to work with, such a class name. For example, for opening FancyClass.cpp and FancyClass.h I would simply write “fancyclass”.
  3. I select all files that I want to open using Ctrl+TAB.
  4. I open each file in a different vim tab by hitting Ctrl+t. It is also possible to open them in window splits with Ctrl+x/Ctrl+v, but I prefer tabs in general.
fzf and vim are good friends

That’s it! It has been possible to improve my workflow with very few requirements and a non-existing learning curve. This has value on its own, because although there are hundreds of useful tools out there, it is often difficult to pick them up and start using them off-the-shelf. Fortunately, in this case fzf delivers quality without any hassle.

If you want to stay updated about useful tips like this, follow me on Twitter (once I create an account)!

PS: Thanks to Pablo Navais for showing me the path to fzf and other wonderful tools.

 

Published inTools

3 Comments

    • Arturo Medina Arturo Medina

      Thanks for the tip Pablo. I will look into it for sure 😉

  1. Nice post, fzf is very handy!! thanks for the tip 😉

Leave a Reply

Your email address will not be published. Required fields are marked *