Interact with this post using Mastodon or
Fuzz is fun: fzf
Published on
Whether you’re dealing with several projects at once or you’re just looking for this single file, it’s useful to be able to get to what you’re looking for quickly and easily. There’s several options out there but they usually rely on your habits (so need time to become efficient) and are limited by the fact that they need to learn your new habits and they are a kind of “evolved” autocompletion but you still need to know the path you’re trying to reach. In addition, this does not allow you to limit your search in a given directory if needed.
When you are looking for a new file, one you don’t access so often, or one you barely remember the name, you end up relying on your dysfunctional brain unless… you know about fzf .
Before digging into fzf
, note that we’ll focus on its terminal use here
but it’s also a great tool to use in (neo)vim (post to come).
╭── What is fzf
?
fzf
stands for fuzzy finder which means that it will look for any files/directories that contain all terms in your search no matter where they appear in the file/directory path.
Let’s take an example to better illustrate that.
╭── How to use fzf
?
Let’s assume you have this tree:
home/
|__ username/
|__ app1/
|__ app1_file1
|__ app1_file2
|__ app2/
|__ subdir/
|__ app2_file1
|__ app2_file2
|__ app2_file3
Running ls $HOME | fzf
will open a customizable
window with the content of $HOME and you’ll see an input box where you can type your search.
If you type app1, the results will be filtered in real time and you’ll only see:
- the directory name app1
- the files app1_file1 and app1_file2
If you type fil, the results will be filtered in real time and you’ll only see:
- the files app1_file1 and app1_file2
- the files app2_file1, app2_file2, and app2_file3
If you type app1 fil, the results will be filtered in real time and you’ll only see:
- the files app1_file1 and app1_file2
With that you should now see how convenient fzf
is and I’ll (soon?) have another post on how to use fzf
as an autocompletion tool to easily browse your files in the terminal.
In the meantime, you can experiment the use of fzf
with all the commands you can imagine. Simply append fzf
after your command (like in the examples above).
╭── Feel like a dev and program fzf
a way you like
fzf
is incredibly customizable and this goes beyond the size and position of the window. Two things you will certainly like tweaking are the preview window and the command to execute on selection.
The preview window
When switching from one item to the other in the results list, you can decide to preview the selected file on the top/right/bottom/left side of the fzf
window.
Here is a random example of the argument to add to the fzf
command:
--preview="if echo {} | grep -Eq \".txt$|.sh$|.md$|.org$|.norg$\"; then cat {} | head -20; \
elif echo {} | grep -q \".zip$\"; then unzip -l {}; \
elif echo {} | grep -Eq \".jpg$|.jpeg$|.png$"; then catimg -H 45 {}; \
elif echo {} | grep -q \".pdf$\"; then pdftotext {} - | head -20; \
else exa {}; fi"'
In order, this will use:
cat
to preview the first 20 lines of any .txt, .md, .org, .norg fileunzip
to preview the content of zip filescatimg
to preview .jpg, .jpeg, .png filespdftotext
to preview the first 20 lines of pdf filesexa
to list anything else
As you’ve noticed, {}
is used as a placeholder for the selected item.
Execution on selection
If nothing specified, hitting Enter will exit fzf
but you can decide to bind Enter to any action you’d like to execute. Use this argument in the fzf
command:
--bind "enter:execute(command {})"
Of course, replace command with the one you want to use.
And that’s not all. You can bind any key to any actions (see details ).
Thanks for your read. Hope it's been useful to you.
✄ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈
More food for thoughts? Check other posts about: #Cli