fzf is one of the most amazing utilities written for the command line in the last few years.
In Linux once you have gotten used to navigating the command line and discover
fzf, you will never go back to not using it.
It’s a utility that does one thing and does it superbly. It’s tiny in size and overhead, results are almost instantaneous with very little memory cost.
But what is it, what does it do and why does everyone rave about it?
In this article we’ll cover how to use fzf, the command-line fuzzy finder, we’ll see how we can customize it, and we’ll explain some concepts, such as what fuzzy search is, so you can better understand how you can use this tool.
We’ll focus on using fzf on Linux, however you can also install it on MacOS or Windows.
Table of Contents
What is a fuzzy search?
A fuzzy search in computer science lingo is an approximate string matching search, a technique for searching strings to match a pattern rather than exact results. A regular exact word search would lookup for
cot and try to find all possible hits with that exact term.
- A fuzzy search for cot would also find the string coat inserting an a
- If searching for coat it will find cot by deleting an a
- And searching for coat will find cost by substituting the a for an s
This closeness is measured and depending on how many elementary operations it needed to get to this result, it knows how close each result is to our original lookup.
So in the example of substituting an a for an s to surface cost from a search for coat, the operations necessary to surface that result was of one, a simple substitute of one character.
In our search example, also the word intercostal was surfaced and the word costume as well. The word costume had three characters inserted at the end so the operations necessary to surface this result was of three characters at the end.
The word intercostal had two characters inserted at the end and five characters inserted at the start, so the operations necessary were of seven characters at two different locations.
So a fuzzy search for
coat would give us the search results of:
In that order as the most approximate possible results. Arithmetically it’s way more complicated than that but this is enough to give us an idea of what a fuzzy search is.
fzf uses the Smith-Waterman algorithm for fuzzy search, by default.
Installing fzf on Linux
fzf is a little open source utility that has no dependencies, it works all by itself. It has support for plugins for Vim and others. It has key bindings to easily access it. It can do fuzzy auto-complete searches. And it has a fast index of previous lookups. It’s all around versatile.
If you’re on a Debian based system like Ubuntu you can simply grab it from the apt repository.
sudo apt install fzf
If you’re on an Arch Linux based system you can install it from the pacman repository.
sudo pacman -S fzf
If you’re on a RHEL based system like Fedora/CentOS/Rocky Linux/AlmaLinux you can installing from the dnf repository:
sudo dnf install fzf
fzf is so popular that it’s available from almost all Linux distributions’ repositories.
Being open source we can also grab it from its home at Github by cloning it and this is what we are going to do in our user home directory.
git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf ~/.fzf/install
In this demo we first use the
cd command to be taken back to our home directory and
git clone a copy of fzf from Github to our computer. We instructed it to copy to a directory in our home folder named
.fzf that it will create.
Then we command it to run the file located at ~/.fzf/install we just cloned, this runs an installation script. We will agree to enable auto-completion, keybindings and to update our shell’s configuration file.
Then finally we source our shell configuration file, most likely either your
.bashrc file or your
.zshrc file depending which shell you’re using, wherever they are located, the default location of these files is your user home directory.
What sourcing does is to update your current shell configuration file, the currently open command line prompt you have right now with the updates the fzf installation script added.
A lot of new users get frustrated and quit when they can’t figure out why the newly installed utilities are not working, and most of the time it’s because the open shell that they are using at the moment hasn’t been updated with the newly updated changes. A good habit is to always source the
.bashrc right after editing the configuration file, or simply close the window and open another command line prompt so the new shell opens with the updated changes.
Like most command line utilities we can start using it by just running it by itself. By running fzf we get an interactive finder.
At this basic form fzf will simply lookup for files in the directory, if the user selects it, it will print out the path of that file. This is the basic function, lookup files and spit them out. The power of fzf comes when we start using and combining its other features.
How to customize fzf fuzzy search configuration
When we authorized fzf to update our shell configuration file what it did was to add this line to it.
[ -f ~/.fzf.bash ] && source ~/.fzf.bash
That tells our shell to always source the file located at ~/.fzf.bash. If we take a look at that file we see it does three things:
- it sources the actual program to the Linux path so that the program is always available to us at the command line
- it also tells it to source the auto-completion and key binding configurations
- if you used
git clone, both would be located at the following location.
(Note: If you installed from a repository it will be at another location.)
Now we know where to find them if we ever want to modify them, but that way would be the hard way of modifying it to our needs.
If we add certain commands in the configuration file of our shell, remember that’s the .bashrc or .zshrc file on our home directory, we can customize fzf to our liking. Here are some basic examples of modifications.
# fzf ctrl-r and alt-c behavior export FZF_DEFAULT_COMMAND="fd --hidden" export FZF_CTRL_T_COMMAND="$FZF_DEFAULT_COMMAND" export FZF_ALT_C_COMMAND="$FZF_DEFAULT_COMMAND – type d"
The first one is the fuzzy search default command, here between the quotation marks we are telling it that instead of using the default Linux
find utility to instead use the fd utility installed in our system.
Don’t know about
fd? fd is another blazing fast finder utility. fd is part of the new breed of utilities like
fzf that are innovating, improving and replacing old stable Linux programs.
How to use the fzf Key Bindings
fzf uses three key combinations, Ctrl T, Alt C and Ctrl R, those were chosen to interfere as little as possible with standard console SIGINT termination inputs that have been used by consoles for decades since the dawn of computing.
So as we learned, we can modify fzf commands or even change them completely in combination with other utilities.
The possibilities are wide and too much to name in a single article. The default behavior of Ctrl T is to fuzzy search, select a file and output its name to be used whatever way we want.
In this example we can see that we wrote the command
open with a
[space] after it, then we Ctrl T and
fzf found via the
fd utility all the files in that directory, then we selected the file we wanted by scrolling down and hitting enter, this outputted the file name of the file we selected, and we then proceeded to hit enter again and execute the command.
Although in this example we scrolled down to select the file, we could have simply used the interactive finder to save even more time by narrowing down the file by typing partially its name.
This behavior alone is a huge time saver.
Just the fact that we can select any file without writing its full name or worse copy and pasting long names, saves us professional Linux users hundreds of hours per year. This is but just one feature and it’s such a useful one that words cannot describe what a quality of life improvement it provides.
Another default key binding is Alt C, it’s default behavior is to
cd into other directories, the
cd command changes our working directory in Linux.
With the default behavior we fuzzy search for a directory, select it in the interactive finder and it will take us to that directory right away. Again this is another huge time saver for the Linux user that spends lots of time on the command line.
In our example we Alt C and fzf find 4 directories, notice that it didn’t give us a list of all files, only those names of directories. Alt C works just with directories.
The behavior of Alt C and Ctrl T can both be modified. Simple modification ideas could be changing that behavior to play video files if you work with lots of videos. Or maybe to open URLS in the default browser in your work requires visiting lots of sites. The possibilities are almost endless.
The last default key binding is Ctrl R, this key combination will allow us to access in an interactive finder the history input commands from our shell history. This is a basic shell behavior as well just in case you didn’t know, but fzf integrations uses its interactive finder and format to give you a list of your inputs in descending order.
While the behavior is the same as the basic shell feature, the execution is much more refined because it uses the fuzzy search results.
In fzf auto-completions are triggered by a simple trigger, the
** characters in sequence. There are lots of utilities that fzf has implemented auto completion triggers for.
You can use it to simply cat a file.
After hitting the
tab key the interactive finder of fzf will come up automatically to be able to select files to cat output.
ssh into a server from a list of servers from your ssh config file.
If you have environment variables that you would like to use, you can for example export right away.
export **<PressTabKey> unalias **<PressTabKey> unset **<PressTabKey>
You can kill processes on your system quickly.
kill -9 <PressTabKey>
All of them are very useful to different people according to each of our needs.
Fzf is an awesome little utility, it’s so well supported and extensible that you will have dozens of uses for it if you want to apply it in your workflow. And even if you don’t extend its functions, the basic features alone are reason enough to start using it. So
git clone it right away or install it via your distribution’s repository and give it a try.
It’s so elegantly designed, it’s what in the Linux world we call a portable application because it can function all by itself with no dependency on any other application.
One last tip for the dedicated readers that read until the end, yes you. If we use Crtl T to fuzzy search for a file to select, we can just type an extension without a file name and fzf will filter all the files by type. Another great functionality that can save us valuable developer time.
fzf is truly an amazing utility, a remarkable piece of software that should be in everybody’s Linux’ box. Thanks for reading and learning with us, don’t forget to follow us.