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.
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
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
$ asdf list-all nodejs | fzf
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.
This will narrow it down to everything that contains
10. Because it is fuzzy, it will include results like
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.
^ 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:
And because this whole thing is interactive, I can quickly change the
0 to a
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 and give it a try.