DEV Community

Cover image for Interactively Browse Large Lists of Data with FZF
Josh Branchaud
Josh Branchaud

Posted on

Interactively Browse Large Lists of Data with FZF

When faced with a large list of output data, I often reach for grep to narrow down the results. This can help me find what I'm looking for, but it often involves a little trial and error.

We can enhance this process by making it interactive. FZF, a fuzzy finding utility, is the perfect tool for the job. It is speedy, composable, and smart.

In this quick video from my Modern CLI series, I show how FZF can be used to interactively narrow down the list of installable package versions output by asdf.

The Details

I needed to find a version of Node.js to install on my machine, but I wasn't sure what was available. Because I use asdf to manage my version of Node, I used the following command:

$ asdf list-all nodejs
Enter fullscreen mode Exit fullscreen mode

This produced pages and pages of output in my terminal. Rather than trying to scroll up to the right spot or get a grep command just right, I decided to pipe the output to fzf.

$ asdf list-all nodejs | fzf
Enter fullscreen mode Exit fullscreen mode

The results of the asdf command are fed into fzf which opens a prompt at the bottom of the terminal. I can type anything that might match the results.

For instance, what if I type 10 into the prompt.

> 10
Enter fullscreen mode Exit fullscreen mode

This will narrow it down to everything that contains 10. Because it is fuzzy, it will include results like 4.10.0, 10.9.0, and even 4.1.0. The results are ordered by best match.

If I want to find Node versions that are major-version 10, then I can inject a little regex into my search term.

> ^10
Enter fullscreen mode Exit fullscreen mode

The ^ character in regex means that the following atom (10) should be anchored to the beginning of the string. Now the results are just those under major-version 10.

I can narrow it further by adding in the minor version:

> ^10.1
Enter fullscreen mode Exit fullscreen mode

And because this whole thing is interactive, I can quickly change the 0 to a 2:

> ^12.1
Enter fullscreen mode Exit fullscreen mode

to find major-version 12 packages.

This is just one example of interactively exploring a large list of output with fzf. Remember that you can pipe output from any command into fzf. Install fzf and give it a try.


There is more where that came from. If you enjoyed this post and screencast, subscribe to my newsletter and check out the rest of the Modern CLI series.

Top comments (1)

Collapse
 
maxdevjs profile image
maxdevjs

+1 subscriber for your series :D

I never really digged into fzf entrails, but I use it everrywhere I can.