Faster command line searches with fzf

Are you pressing the up and down arrows of your keyboard while looking for a command you previously run in the shell? Then you probably know how many keystrokes you have to press to find what you are looking for. Stop digging holes on your keys! In this post we are going to install a nice tool called fzf and we are going use it to speed up many common command-line operations.

fzf is a general purpose interactive command line fuzzy finder, used to filter large set of data in the shell. It basically allows to search for something without providing a 100% match. For example, if we are looking for files with the word core in their name, a fuzzy finder will match all files including the word core, but also files like cat-on-tree, as the letters of the word core are included in the proper order (searching for an exact match is just an extra character, we’ll see that later in this post).

fzf ready to accept our commands
fzf ready to accept our commands

Let’s start with the installation for macOS, for other platforms you can find detailed instructions here. We are going to use Homebrew:

brew install fzf

Then you need to run the installer using the following command:

$(brew --prefix)/opt/fzf/install

This command will also ask you to enable fzf autocompletion and keybindings, and to update your shell configuration files (it basically adds a line in your .bashrc o .zshrc).

Good, now it’s time to use our new superpowers.

Looking into the commands history

You are looking for a command you typed time ago in your shell. You can press the top-arrow multiple times, going through all commands you executed between the command you are looking for and the last, but that’s terribly slow. Or you can use ctrl+r, which now presents a totally different output.

Looking at the command history
Looking at the command history

You can type for example ls and fzf will filter the results showing only the command with the word ls included.

Filtering using a keyword
Filtering using a keyword

You can move up and down between fzf results, using the up-arrow or down-arrow key, or even better using ctrl+j and ctrl+k to move up and down without leaving the comfort of the home row.

After finding your match you can press ENTER to close fzf and move the command inline, and ENTER again to run the command.

Finding files or directories

Suppose you are looking for a file or a directory in the current folder or its child. Most shells have an autocomplete mechanism that allows you to press TAB to automatically complete a filename or a filepath, but that requires you to write the exact filename or filepath from the beginning.

With fzf you can press ctrl+t to open the fuzzy finder, getting the list of all files available in the current directory and its child folders.

Looking for files
Looking for files

You can then filter as you do with commands and then press ENTER to confirm your selection.

Providing files as arguments to commands

Some commands like rm allows for a list of files to be provided as arguments, for example rm file1 file2 deletes both files. fzf comes to help also with this, as you can select multiple files from its prompt.

First of all type rm, then press ctrl+t to call fzf. Then move with the arrows on one of the files you want to delete and press TAB to select it. A little > appears on the left of that file. Don’t press ENTER yet, resume your search and look for another file, press TAB when you find it. When you are done press ENTER and both filenames will be displayed inline after rm, ready to be deleted.

Select two files with TAB
Select two files with TAB

Smart command completion

fzf is smart enough to provide useful completions to some popular commands. For example, type cd and then the completion trigger sequence (default **), then press TAB. fzf will show you only the available directories to complete that command.

Smart command completion
Smart command completion

This will work also with vim, if you type vim ** and press TAB, and many other commands.

You can view some other supported commands here, like kill -9, unset, export. The one I personally use the most (except cd and vim) is for sure ssh ** then TAB, to autocomplete using my hosts file.

Extended mode

fzf starts by default in Extended mode, this means we can use some tricks to get better results from our fuzzy search. Let’s see some examples:

  • Search for the exact match of core, use 'core.
  • Search for all files ending in .css, use .css$.
  • Search for all files starting with song, use ^song.
  • Invert the search pattern, prepend ! in front of your search (works with this others matchers too).

Wrapping up

fzf is an incredibly useful everyday companion that you will miss whenever it’s not available. For now we only touched the basics, but you can do many other things with it, like the integration with a text editor for a super quick file search (vim support is well supported). Try it, you won’t be disappointed.