Enhanced searches with fzf
Command Line – fzf
![© Photo by Timothy Eberly on Unsplash © Photo by Timothy Eberly on Unsplash](/var/linux_magazin/storage/images/issues/2023/268/fuzzy-finder/photobytimothyeberlyonunsplash_llama.png/817238-1-eng-US/PhotobyTimothyEberlyonUnsplash_llama.png_medium.png)
© Photo by Timothy Eberly on Unsplash
Simplify your searches and get better results with fzf, a modern search tool based on fuzzy logic.
Search commands have always been essential to using computers. With the increased storage capacity of modern systems, they are more important than ever because there is more material to search. In fact, searches are so important that alternatives to time-honored commands are becoming increasingly common. One of the most effective of these recent alternatives is fzf
, a command-line "fuzzy finder" [1].
For most Linux users, the basic search tool for finding directories and files has been find
, which is most effective when you know exactly what you are looking for. If you do not know, then you need a way to allow variation in the output. Traditionally, variation in search tools like find
is provided by regular expressions (regexes), a concept first developed in the 1950s. Regexes are standard patterns that offer different types of variations in results. Most users are familiar with the simpler regexes, such as *
to mean zero or any character, so that apt*
might return apt
, apt-get
, apt-cache
, and probably many others. At a more advanced level, users might know that gray|grey
produces results with either spelling, or [b-e]
a range of characters. However, regexes are not consistent across applications and can rapidly become extremely complex to construct or type correctly, especially when all you want is a quick answer. For example, even with context, who can easily explain this randomly selected example:
(\W|^)[\w.\-]{0,25}@(yahoo|hotmail|gmail)\.com(\W|$)
Personally, I'd take half a minute or so and resent the expected effort. Such regexes are worth constructing only when you use the same search repeatedly. In addition, you almost always will find scanning fuzzy logic results quicker than entering a lengthy regex, even if you type the regex correctly the first time. With fzf
, you interact with the tool through a fuzzy logic interface, while fzf
composes the regexes for a search behind the scenes.
Fuzzy logic is a science of its own, with principles and theories about communication and logic that do not directly concern us here [2]. What matters is that fuzzy logic is a much simpler way of introducing variation into output, especially today when the patience and general command-line knowledge among computer users is much less than it was 70 years ago. For one thing, the order in which variation is found with fuzzy logic is less important than with regexes. Often, the order does not matter at all, which makes it easier to understand and more flexible. Just as importantly, fuzzy logic uses natural language instead of its own code that needs to be carefully separated from the query itself. If you add an indexed database, as fzf
does, you can add context to accuracy to produce a contextual command history.
With these advantages, fzf
has rapidly gained ground in the past few years. It is carried in all the major distributions, or it can be installed from its GitHub page. Each time fzf
opens using the bare command, it updates its database in a matter of seconds, leaving you at a prompt, ready to continue.
Setting Up fzf
The popularity of fzf
means that, for most purposes, you can install it directly from your distribution's repositories and begin to use it. You can also install fzf
with Homebrew to get the absolute latest version.
However, to make full use of fzf
, you should also install bat
(the modern cat
replacement) and fd-find
(the modern find
replacement). Then add the following lines to your .bash.rc
files:
export FZF_DEFAULT_OPTS="--preview 'bat --color=always {}'" export FZF_DEFAULT_COMMAND="fd --type f"
These lines will ensure that fzf
can use bat
as a file previewer and improve overall performance with fd-find
. Faster than find
, fd-find
will ignore directories like .gitignore
while searching. In addition, add
$ source /usr/share/fzf/shell/key-bindings.bash
to enable some useful keyboard shortcuts (see Table 1).
Table 1
Keyboard Shortcuts
Ctrl+T |
Select files |
Alt+C |
Switch into a selected subdirectory |
Ctrl+R |
Use |
Basic Use
At its simplest, fzf
opens and updates its list of top-level directories for the present working directory – usually, your home directory. You can then type any part of a directory or file to see the results (Figure 1). If you scroll the results, highlighting and pressing the Enter key displays the selection in the terminal, where it can be conveniently pasted into newly typed commands. Alternatively, you can use fzf
with another command so that
vim $(fzf)
allows you to use fzf
to select a file to open in Vim.
For many uses, this is enough information to use fzf
. However, depending on how fzf
is compiled, it can also support context-specific autocompletions by entering a command followed by **+Tab
and will give appropriate suggestions, such as file names for Vim or remote systems for SSH. If autocompletion is not enabled, check the project's GitHub page for instructions.
Other Command Options
Most online articles on fzf
end at this point. However, a few distributions, such as Ubuntu, offer man pages with additional options, neatly organized into categories. Among the most immediately useful are the search modes. Some of these options may be familiar to you from find
and other search commands. For instance, with +1
, searches become case-sensitive, while you can reduce clutter in the results with --exact
(-e
). In addition, --algo=TYPE
sets the fuzzy algorithm, completion with v1
optimizes speed over best results, and v2
optimizes results over speed.
Another useful option category is for search results. The arrangement of results can be influenced by --tiebreak=CRI CRITERIA, CRITERIA
. The default criteria is length, which favors shorter results, but you can give it lower priority by its position in a list that can contain begin
(with the search string closer to the start of the line), end
(with the search string closer to the end of the line), or index
(a line that already appears in the results). Each criterion should only appear once, and index
(for obvious reasons) must always appear last. If you are searching for high-level directories, start
might be a useful option or end
if you are searching for a file name. In addition, --tac
displays results in reverse order.
Other options are cosmetic, such as disabling the mouse or horizontal or vertical control, or aspects of appearance, such as color theme or window border size for previews. These options no doubt become more important the more often you use fzf
, but at first glance give a false sense of complexity for what is after all a relatively well-organized utility.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
![Learn More](https://www.linux-magazine.com/var/linux_magazin/storage/images/media/linux-magazine-eng-us/images/misc/learn-more/834592-1-eng-US/Learn-More_medium.png)
News
-
NVIDIA Released Driver for Upcoming NVIDIA 560 GPU for Linux
Not only has NVIDIA released the driver for its upcoming CPU series, it's the first release that defaults to using open-source GPU kernel modules.
-
OpenMandriva Lx 24.07 Released
If you’re into rolling release Linux distributions, OpenMandriva ROME has a new snapshot with a new kernel.
-
Kernel 6.10 Available for General Usage
Linus Torvalds has released the 6.10 kernel and it includes significant performance increases for Intel Core hybrid systems and more.
-
TUXEDO Computers Releases InfinityBook Pro 14 Gen9 Laptop
Sporting either AMD or Intel CPUs, the TUXEDO InfinityBook Pro 14 is an extremely compact, lightweight, sturdy powerhouse.
-
Google Extends Support for Linux Kernels Used for Android
Because the LTS Linux kernel releases are so important to Android, Google has decided to extend the support period beyond that offered by the kernel development team.
-
Linux Mint 22 Stable Delayed
If you're anxious about getting your hands on the stable release of Linux Mint 22, it looks as if you're going to have to wait a bit longer.
-
Nitrux 3.5.1 Available for Install
The latest version of the immutable, systemd-free distribution includes an updated kernel and NVIDIA driver.
-
Debian 12.6 Released with Plenty of Bug Fixes and Updates
The sixth update to Debian "Bookworm" is all about security mitigations and making adjustments for some "serious problems."
-
Canonical Offers 12-Year LTS for Open Source Docker Images
Canonical is expanding its LTS offering to reach beyond the DEB packages with a new distro-less Docker image.
-
Plasma Desktop 6.1 Released with Several Enhancements
If you're a fan of Plasma Desktop, you should be excited about this new point release.