Skip to main content

Find anything you need with fzf, the Linux fuzzy finder tool

I'm thankful for the Linux fuzzy finder tool because it superpowers the command line by making it fast to find whatever I'm looking for.
Image
Binoculars on a blurry background

Photo by Skitterphoto from Pixabay

One of the aspects of the open source community that excites me the most is having the opportunity to interact with people from different backgrounds, learn, and share experiences with them. During these interactions, I often learn about new ideas, improved ways to perform my job, and new technologies and tools.

Some of these tools can break paradigms and change the way you work. For me, one of these tools is fzf, a command-line "fuzzy finder" developed in Go.

[Cheat sheet: Old Linux commands and their modern replacements ]

If you've seen some of my other Enable Sysadmin articles, then you're probably aware that I'm a big fan of using the Linux command-line environment. The fzf command significantly improves your terminal experience and increases your productivity. fzf is so versatile and powerful that I often say it gives your command line magic powers.

What is fzf?

In its most basic form, fzf works as a filter, processing text from standard input (stdin), then outputting a matching selection to standard output (stdout). In a way, fzf is similar to grep but it adds features that make it more suitable for interactive searches, including:

  • Fuzzy finding capabilities allow approximate searches by any part of the match in any order.
  • Super-fast processing speed
  • Interactive match selection lets the user select the desired match after initial filtering.
  • Single- or multi-text selection
  • File content previews
  • A comprehensive set of features and customization options

Due to these features, fzf is a fantastic option when you're searching anything on the terminal, for scripts, and to develop pseudo user interfaces for command-line programs.

When I think about it, I am still in awe that I have access to such a fantastic tool from the open source community. I am grateful that Junegunn Choi and other contributors made it available for others to benefit.

[ Get the guide to installing applications on Linux. ]

Install fzf

Because fzf is developed in Go, you install it by downloading the binary application straight from the project's Releases webpage. However, to experience its full benefits, you should also download the shell extensions files to add new key bindings and autocompletion for your shell. Alternatively, you can install the package version that contains everything from your distribution's repository. For example, for Fedora, install it using dnf:

$ sudo dnf -y install fzf

I also recommend you install two additional tools that will improve your experience with fzf: fd-find, a faster and improved version of the find command, and bat to preview file contents. For more information about bat, take a look at Seth Kenlon's article Drop the Linux cat command for bat.

Install these packages in Fedora using dnf:

$ sudo dnf -y install fd-find bat

Now that you have fzf and those prerequisites installed, take a look at how it can improve your terminal experience.

Get started using fzf

To start with fzf, run it without any arguments. By default, fzf searches all files under the current directory, allows you to filter and search interactively, then prints the result to standard output. For example, to search for the default Pipewire configuration file without knowing where it's located, you can use fzf starting from your root directory:

Notice that fzf updates the matches dynamically while you type the search terms in any order.

Also, because fzf prints the selection into standard output, it gives you unlimited flexibility on how to use its output. For example, imagine you want to edit the file above with Vim. You could use fzf to obtain the file name and pass it like this:

$ vim $(fzf)

Or you could use it to copy the file to a different location:

$ cp $(fzf) ~/.config/pipewire

The possibilities are endless, but fzf adds more niceties to improve your productivity even more.

Previewing file content

Another nice fzf feature is the ability to preview each file as you navigate the interactive selector. Previewing files as you search gives you an extra chance to find what you're looking for, particularly if you're not sure which file you want.

You can use any command you want to preview. You can also use scripts to make it smarter and change according to the selected item. For example, to use bat to preview files, call fzf using the --preview command line flag:

$ fzf --preview 'bat --color=always {}'

To avoid typing this option every time, you can set the environment variable FZF_DEFAULT_OPTS to apply it automatically every time you run fzf:

$ export FZF_DEFAULT_OPTS="--preview 'bat --color=always {}'"

You can also set this environment variable on your shell initialization script, such as .bashrc for Bash, to make it permanent.

[ Download now: A sysadmin's guide to Bash scripting. ]

Change the default search command

By default, when you execute fzf with no arguments, it uses the find command to search for files. For faster and more feature-rich searches, you can replace the default search command with fd by setting the environment variable FZF_DEFAULT_COMMAND like this:

$ export FZF_DEFAULT_COMMAND="fd --type f"

fd is quicker and smarter than find, ignoring directories like .git and using your .gitignore options while searching. It considerably improves the experience.

For permanent changes, set this environment variable in your shell initialization script.

Use autocompletion

Another fzf feature that helps improve your terminal productivity is context-aware autocompletion. To enable this feature, you either need to install fzf using the package or download the corresponding completion file from the repository and enable it.

If you installed fzf using the Fedora package, autocompletion is automatically enabled. Restart your terminal after installation to ensure it loads the autocompletion options.

To use autocompletion, type ** and press Tab in front of a command to launch fzf with a list of context-relevant suggestions. For example, if you use it in front of a command that expects a file, such as vim, fzf lists files. On the other hand, if you use it with ssh, then fzf lists possible remote systems to connect to from your hosts or SSH config files.

$ vim ** <TAB>

For SSH:

You can also use fzf autocompletion to complete environment variables or to find processes in the context of the kill command:

For more examples of how to use and customize this feature, take a look at the project's documentation.

[ Keep your most commonly used commands handy with the Linux commands cheat sheet. ]

Superpower your command line with fzf key bindings

Using fzf directly or via autocompletion is great, but adding key bindings makes it even better. To enable key bindings, source the corresponding file for your shell. For example, for Bash, source /usr/share/fzf/shell/key-bindings.bash:

$ source /usr/share/fzf/shell/key-bindings.bash

You can also add this line to your shell initialization file to make it permanent. If you don't have this file, download it directly from the project's repository.

Once you load this file, you can use three key bindings for quickly triggering fzf:

  • Ctrl+t: To select files, similar to autocompletion but with fewer keystrokes
  • Alt+c: To quickly switch into a selected subdirectory

    For example, imagine you want to switch to the directory dnsmasq.d under /etc/NetworkManager, starting from the root directory:
  • Ctrl+r: For smart searching your command history

    This is one of the most useful fzf applications. This key binding replaces the default history search with a smart search that makes you more efficient at retrieving previously executed commands.

    For example, using the standard history search, you need to type exactly what you're looking for, or the search fails. It's hard to find long previous commands this way. Using fzf gets where you want with a few keystrokes:

Using these key bindings and fzf's blazing-fast search, you can move around your command line much more efficiently, as well as find and execute previous commands in no time. With a bit of practice, it feels like you have new superpowers.

Other ways to use this powerful tool

fzf is a powerful and versatile tool that helps me improve my command-line efficiency dramatically. I am grateful to have access to such a tool.

fzf can do much more than what I explored in this article. It follows the Unix philosophy of doing one thing and doing it well. But this one thing allows you to use fzf in different ways.

In my next article, I will show how to use fzf for other useful purposes, like filtering packages or as a pseudo user interface for scripts. You can also customize fzf to change its appearance, colors, position, and more. If you can't wait, take a look at the project's wiki page for more ideas and examples from the community.

[ No-cost online course: Red Hat Enterprise Linux technical overview. ]

Topics:   Linux   Command line utilities  
Author’s photo

Ricardo Gerardi

Ricardo Gerardi is Technical Community Advocate for Enable Sysadmin and Enable Architect. He was previously a senior consultant at Red Hat Canada, where he specialized in IT automation with Ansible and OpenShift.  More about me

Try Red Hat Enterprise Linux

Download it at no charge from the Red Hat Developer program.