Linux experts Seravo background Linux Debian SUSE
Linux-natives blog: Linux and open source – technology and strategy

Save your time on the command-line with fuzzy finder

The command-line fuzzy finder by Junegunn Choi is a new interesting time saver and power user utility I find myself using all the time nowadays.

Search the command history with fzf

A very common thing for command-line power users is to use Ctrl+R to search the command history, and thus avoid typing the same command over and over. But what if you remember parts of the command, but not in the correct order? Say I want to run dstat with --top- something, but I don’t remember if I ran dstat --top-cpu or maybe dstat -n --top-cpu. If there were some characters in between, a normal Ctrl+R search on dstat --top- would not find it. This is a use case where fzf excels. With it, I can simply press Ctrl+R and write dstat top and it will list all occurrences that had those words anywhere. I can simply browse the results with the arrow key and press enter to select what I’m looking for, and another enter to run it. Simple and quick!

Command-line fuzzy finder demo on Ctrl+R use
Command-line fuzzy finder demo on Ctrl+R use

If you are wondering which prompt is used in the recording above, check out our post on Liquid Prompt!

Jump immediately to correct directory with fzf

Surely all command-line users have the experience of typing cd and ls all the time. What if you could find the correct directory and jump immediately to it with no effort? Just use the powers of fzf. When you have it installed, you can simply type Alt-C to activate the auto-complete with which you can conduct a search with one or more file names. In the demo below I want to go to the modules directory in the nginx source code. I just type nginx module and fzf presents me with the exact thing I wanted. Effortless!

Command-line fuzzy finder demo on double wildcard use
Command-line fuzzy finder demo on double wildcard use

Find and manipulate multiple files effortlessly

Another very common scenario on the command-line is to view or edit multiple files. Finding the files and writing the paths to each file can be slow. But not with fzf! Simply type the name of your command (in this example bat to view files) and then press Ctrl+T to trigger the file searcher and selector. Type the name you want to search with, use arrow keys to browse the list and press tab to select the files you want to return. Once you’re done, just press enter, and you have the command completed. Press enter again to launch it.

Command-line fuzzy finder demo on Ctrl+T use
Command-line fuzzy finder demo on Ctrl+T use

Fzf can work with any list you give it

By default, in file operations fzf uses a list retrieved by running find . in the current directory. There is nothing that inherently limits fzf to files. There are a lot of use cases where you want to give it a list manually, and then apply a search based on that. In the demo below a list from git branch is sent to fzf, so it is easy to find that old branch by typing in but a couple of characters.

Demo of running fzf manually
A list of git branches sent to fzf manually to search from

In the demo above, you can also note that fzf looks a bit different here. That is because it was customized with export FZF_DEFAULT_OPTS='--height 40% --layout=reverse --border'.

There is more

If typing ssh ** and then pressing tab, fzf presents a list of SSH hosts to choose from. The double wildcard will most of the time translate to a file list dialog, but in case of compatible software, fzf can show you a dialog, allowing you to select from the autocomplete options. There is a seamless integration for e.g. kill so you can type in process names, and out comes the process IDs.

Demo of fzf completing process ids to kill

What if your query is more complex? No worries, just type your query as a regular expression or using some of the special syntax fzf offers!

Read the Usage section in the README of fzf to learn more. There are also many configuration options to choose from, such as window height, direction, margin, borders, file list generation methods, search result sorting etc. For a full list of all options, see the man fzf page.

Installation

If you’ve read this far, you probably want to install fzf and try it out yourself. The older version 0.17 is available in current stable Debian, and a bit newer 0.20 in Ubuntu releases. Simply run apt install fzf to get it on a Debian based system.

To activate it in your shell, add the following lines to .bashrc:

# fzf keybindings for Bash:
source /usr/share/doc/fzf/examples/key-bindings.bash
# fuzzy auto-completion for Bash:
source /usr/share/doc/fzf/examples/completion.bash

Open a new session and start fuzzing away!

Written by

Linux-natives – a blog by Linux experts from Finland – is brought to you by Seravo, a Finnish company focused on open source software and services.

Our team provides premium hosting and upkeep for your WordPress website - with open source software.

2 thoughts on “Save your time on the command-line with fuzzy finder

  1. Jonas Smedegaard says:

    Thanks for this nice introduction to fzf!

    One detail: You suggest source’ing example files below /usr/share/doc – please consider changing that to a two-step routine of first _copying_ those example files and then source’ing from the local copy.

    Reason is that example files in Debian packages are not to be relied upon as being stable.

    See https://www.debian.org/doc/debian-policy/ch-docs.html#examples and notice the phrasing “for documentation only”.

    If you think that the Debian fzf package ships example files worthy of regular use – i.e. not “for documentation only”, then file a wishlish bugreport suggesting that the package ships those files somewhere else – e.g. below /usr/share/fzf/ (and provide them as symlinks below /usr/share/doc/fzf/).

    Users of fzf might also find the competitor fzy interesting.

    1. Otto Kekäläinen says:

      Thanks for the insightful comment Jonas!

Leave a Reply

Your email address will not be published.