fzf user guide

fzf is a command line search/list tool.

here is a short explanation - demonstrated with a simple example - how i use it within my package manager UI called PacUI..

prerequisites:

an installed fzf version and basic command line knowledge

basics:

lets start by printing a list of all installed programs to the terminal:

pacman -Qq

next, use fzf to make this list search and selectable:

pacman -Qq | fzf

start typing to narrow down the list of choices. use arrow keys to move the selction marker. select a line with ENTER (selected line(s) get printed to the terminal). cancel with ESC or CTRL+C.

next, i search case insensitive and enable the user to select multiple lines (using TAB key):

pacman -Qq | fzf -i --multi

i want the list reversed. this makes fzf better fit in PacUI:

pacman -Qq | fzf -i --multi --reverse

in order to search and see the number of filtered list entries in a single line (safe vertical space), i use:

pacman -Qq | fzf -i --multi --reverse --info=inline

i also want to use a short explanation for the user before the search field and below. therefore, 2 more arguments need to be added:

pacman -Qq | fzf -i --multi --reverse --info=inline --prompt="Enter string to filter list > " --header="TAB key to (un)select. ENTER to install. ESC to quit."

preview window:

the preview window is an advanced option in fzf. it can display a static text or execute a command. parts of the command can contain the selected line. you need to use the wildcard {} where you want the content of the selected line to be.

i want to display package information using the command:

pacman -Qi <package name>

therefore, lets use:

pacman -Qq | fzf -i --multi --reverse --info=inline --prompt="Enter string > " --header="TAB key to (un)select. " --preview ' pacman -Qi {} '

in order to make the outline of the preview window appear in ANSI instead of unicode characters

pacman -Qq | fzf -i --multi --reverse --info=inline --prompt="Enter string > " --header="TAB key to (un)select. " --preview ' pacman -Qi {} ' --no-unicode

i want the preview window either at the right side (when the width of the terminal is more than 125 columns) or at the bottom. the following command prints (but not executes the correct fzf argument for that:

if (( $(tput cols) >= 125 ))
   then
      echo "--preview-window=right:55%:wrap"
   else
      echo "--preview-window=bottom:60%:wrap"
fi

wrapping this command in "$( ... )" makes BASH interpret the command. therefore i use:

pacman -Qq | fzf -i --multi --reverse --info=inline --prompt="Enter string > " --header="TAB key to (un)select. " --preview ' pacman -Qi {} ' --no-unicode "$( if (( $(tput cols) >= 125 )); then echo "--preview-window=right:55%:wrap"; else echo "--preview-window=bottom:60%:wrap"; fi )"

even more arguments:

i use additional arguments for different purposes. such as:

  • make fzf look better:
    --margin="4%,1%,1%,2%"

  • speed improvements:
    --no-sort --select-1

  • let the selection marker jump over the start/finish of the list:
    --cycle

  • preenter a string AA into fzf's search field upon start:
    --query="AA"

  • use "exact" instead of fuzzy search mode. i find this to yield better results:
    --exact

  • make fzf interpret ansi escape sequences (which can adjust the font) in its list:
    --ansi

questions:

i hope i could explain a little bit how i use fzf and why i use it this way. feel free to ask further questions.

9 Likes

These are my most used fzf commands:
View your pacman log:

cat /var/log/pacman.log | grep -iE 'installed|upgraded' | fzf -e +s --reverse

A zshell function to search your history:

fh () {
        print -z $( ([ -n "$ZSH_NAME" ] && fc -l 1 || history) | fzf -e +s --tac | sed 's/ *[0-9]* *//' | sed 's/ *[*]* *//')
}

Browse installed packages:

pacman -Qq | fzf --preview 'pacman -Qil {}' --layout=reverse --bind 'enter:execute(pacman -Qil {} | less)'
3 Likes

here are tons of examples of other usages of fzf:

i use at least a couple of them in shell aliases / functions (but most of them are modified to my taste).

3 Likes

I zero experience with Fuzzy Logic much less even used file search tools that implement the technology.

I just check but PacUI I don't see anywhere when called up Pamac. Not even in the AUR.

1 community/pacui 1.12-1
    Bash script providing advanced Pacman and Yay/Pikaur/Aurman/Pakku/Trizen/Pacaur functionality in a simple UI
2 community/pacui-git r943.6da718f-1
    Bash script providing advanced Pacman and Yay/Pikaur/Aurman/Pakku/Trizen/Pacaur functionality in a simple UI

pacman -Qq | fzf --preview '\''pacman -Qil {}'\'' --layout=reverse --bind '\''enter:execute(pacman -Qil {} | less)'\' got unmatched.

image

image

Works here - are you using zsh?

Yeah, I'm using zsh. Maybe some setting leads to that?

pacman -Qq | fzf --preview 'pacman -Qil {}' --layout=reverse --bind 'enter:execute(pacman -Qil {} | less)'

Wonder if copy/paste screwed up?
Yes, have amended my post. Weird!

Yeah, now I works!

1 Like

Looks like I copied an additional '\', sorry about that :smiley:

Moved from #general-discussion to #technical-issues-and-assistance:tutorials

Forum kindly sponsored by