DEV Community

Roberto Dip
Roberto Dip

Posted on • Originally published at monades.roperzh.com on

Weekly Command: Fuzzy finding everything with fzf

The Basics

fzf [--options]
Enter fullscreen mode Exit fullscreen mode

fzf is an interactive fuzzy finder, it reads a list of items from STDIN, and writes the selected item to STDOUT.

If no input is provided via STDIN, fzf by default will use the find command to fetch the list of files excluding hidden ones.

fzf fits very well into to the *nix philosophy, which gives it a lot of flexibility, including a lot of built-in goodness for bash and zsh shells (head to the [examples][#examples] for more).

The program is portable, has no dependencies and can be installed easily following the instructions in the README of the project.

Examples

Without further introduction, here are some examples of fzf in action.

Bare

fzf-bare

As stated earlier, fzf can be invoked on its own, and it will provide fuzzy completion for the items found in the current directory and subdirectories.

Previewing

fzf-preview

$ fzf --preview="head -$LINES {}"
Enter fullscreen mode Exit fullscreen mode

In addition, you can provide a --preview flag with a command to execute when a file is selected. The command string accepts placeholders to be replaced by fzf during file selection:

  • {} single-quoted string of the current line.
  • {+} space-separated list of the selected lines (or the current line if no selection was made) individually quoted.
  • {q} current query string.

Fuzzy completion for commands

fzf-asterisk

# Files under current directory
$ cat **<TAB>
# Processes
$ kill -9 <TAB>
# Host names
$ ssh **<TAB>
# Environment variables / Aliases
$ export **<TAB>
Enter fullscreen mode Exit fullscreen mode

A great feature which comes bundled-in for bash and zsh is fuzzy completion for different commands, in most cases just typing ** and pressing (TAB) will give you the fuzzy completion interface to feed the command with the selected items.

On bash, fuzzy completion is enabled only for a predefined set of commands via the complete built in, but you can enable it for other commands with:

complete -F _fzf_path_completion -o default -o bashdefault <COMMAND_NAME>
Enter fullscreen mode Exit fullscreen mode

Searching history

fzf-history

Also by default in bash and zsh, there’s built-in support for the reverse incremental history search.

Further configuration

You can provide additional configuration options via environment variables:

  • FZF_COMPLETION_TRIGGER helps you to define a custom trigger sequence instead of the default **
  • FZF_COMPLETION_OPTS allows you to define and provide custom default flags
  • FZF_DEFAULT_COMMAND default command to use when the input is tty

Sources

Top comments (0)