Weekly Command: Fuzzy finding everything with fzf
Tags: weeklycommand, xnix
Published: 2018-04-26 21:38:39

---
title: Weekly Command: Fuzzy finding everything with fzf
published: true
tags: weekly-command,xnix
canonical_url: https://monades.roperzh.com/weekly-command-fuzzy-finding-everything-with-fzf/
---

## The Basics

```
fzf [--options]
```

`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`](http://man7.org/linux/man-pages/man1/find.1.html) 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](https://github.com/junegunn/fzf#installation) of the project.

## Examples

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

### Bare

![fzf-bare](https://user-images.githubusercontent.com/4419992/39314537-d868a596-494a-11e8-9adf-aa6043912510.jpg)

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](https://user-images.githubusercontent.com/4419992/39314540-d8d3e73e-494a-11e8-937e-85e8396b95f8.jpg)

```
$ fzf --preview="head -$LINES {}"
```

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](https://user-images.githubusercontent.com/4419992/39314536-d83dd348-494a-11e8-8b7c-0e4af0186114.jpg)

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

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`](https://www.gnu.org/software/bash/manual/html_node/Programmable-Completion-Builtins.html#Programmable-Completion-Builtins) built in, but you can enable it for other commands with:

```
complete -F _fzf_path_completion -o default -o bashdefault <COMMAND_NAME>
```

### Searching history

![fzf-history](https://user-images.githubusercontent.com/4419992/39314538-d898951c-494a-11e8-8032-22a84a449106.jpg)

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

- [`fzf` repo](https://github.com/junegunn/fzf)
- [vim plugin](https://github.com/junegunn/fzf.vim)