Fzf: A tool that will transform your CLI life

Suraj Pillai
6 min readAug 2, 2022

--

I’m a CLI junkie, addicted to Vim motions, and never miss an opportunity to bring those two in to any part of my workflow. Naturally, I love to geek out about command line utilities and am always on the lookout for the next tool that can improve my productivity or just make CLI more fun to use. I can confidently say that Fzfis one of the handful of tools I’ve discovered over the years that has done both and has,thus, significantly improved the quality of my command line life.

Imagine you have to look for a file within a directory. find is most people’s go-to tool for this. You type infind . -name <filename> . If you’re lucky, you get the file you are looking for, copy the path and then feed it to the next command. Sometimes, you aren’t that lucky and the search returns no results. You then use a wildcard search and get a bajillion results. You sift through those and find maybe a few that strong contenders. You then use lessor cat to open each one up and, finally, get to the file you want. Phew! Now imagine doing this 10 times a day, or more… Can you really blame those who want nothing to do with a terminal?

Enter fzf. Let’s imagine the same scenario again where you want to find a file within the current directory. You hit ctrl+t on your keyboard and some files within the directory are listed for you. You can also see the tool “indexing” more files as it continues to build up the list. You type in a part of the your file name and the list magically starts re-ordering based on the closest matches to the name you typed. You also notice that it’s fast! Didn’t get the name right at first? You delete the current search term and retype the correct name (or parts of it) without the list ever leaving your screen and adjusting itself in real-time as you type. In fact, you can also type in the name of a subfolder as part of your search term to narrow down your search. But wait, what’s this? As you cycle through the list of files you see a preview of each file’s contents in a separate pane! No more copying the file name and using less to view its contents. And what’s more, you can hit ctrl+t in the middle of a command, fuzzy search and select the file you want and continue typing on as if nothing happened. Suddenly, you don’t mind doing this multiple times a day.

Interactive fuzzy search for files. Source: https://github.com/junegunn/fzf

Now while this is great, merely being able to search for files faster isn’t enough for a tool to be transformative. So, why do I feel so strongly that it is? Because fzf is not merely a tool to help you search files quicker, it is a framework that can be applied to any list to yield transformative results. That framework being, in addition to fuzzy searching fast through a list, you can run commands on the element under your cursor and display the results in a preview window as you scroll through the results. Not convinced yet? Let’s look at some practical applications

How do you search for packages in a debian distribution? You either google to get the right package name or use apt search . But then you could have multiple versions of the same package and sifting through the results to find the right version to install can be quite cumbersome. With fzf ,however, you could write a script to feed the results of apt search <packagename> to fzf and show the package info in the preview window as you scroll through the results! And you can bind that script to a key combination (if you frequently install packages) or an alias so the next time you have to install a script you can fuzzy search for the exact package and version you want and use the description to confirm it is indeed what you are looking for.

Interactive fuzzy searching for “apt” packages

Here’s another example that’s particularly close to my heart because it was a fairly complex script I had to write and one that I use most often and has had the most impact on my workflow. As a Salesforce developer, I use the Salesforce CLI a lot for my work. The CLI is based on the oclif framework and has commands that are notoriously verbose. Plus each command has several switches that don’t always have the most descriptive names or consistent chars. And while it does have an autocomplete feature, it still requires you to know the first parts of the command you are looking for and you have to type the common prefixes for each command out. With my script, I can fuzzy search through the commands and show the help doc and examples for each as I cycle through the list. I’ve the script bound to a key combination. It takes a lot fewer keystrokes for me to get to the command I want, even when I don’t exactly know what I am looking for. After selecting the command, I can use the same key combination to fuzzy search through the switches and preview the description of each switch for that specific command. I cannot possibly overstate how much this has helped transform the quality of my life with the CLI as a developer! As more commands keep getting added to the CLI with each release, fzf also lets me discover new commands and read the descriptions and examples for each without ever leaving the terminal.

fuzzy autocomplete for the Salesforce CLI

Since fzf is as much a framework as a tool, it has spawned an entire ecosystem of scripts and plugins for all major shells. You don’t have to take my word for it. The fzfplugin for Vim, developed by the author themselves, is notable and elevates Vim’s usability to a whole new level. From fuzzy searching files, buffers, snippets, combing through git logs or fuzzy searching within a file itself, its the swiss army knife of Vim plugins.

Fuzzy search words and definitions in Vim/Neovim using Thesaurus and wordnet

As powerful as fzf scripts and plugins are, in order for these to be truly transformative, you have to use key bindings for the scripts that you use most often and aliases for the ones you use less frequently.

Hopefully, by now you have a better understanding of the power of this deceptively simple tool and are eager to give it a whirl. Maybe you even have a couple of ideas for your own scripts as well! And if you’re a visual person and text just doesn’t spark your imagination the same way, here’s an excellent video that had inspired me to dive deeper into what this tool can do. Here’s another one recommened in the fzf readme.

--

--

Suraj Pillai

Salesforce Architect and Tech Enthusiast. Views are my own.