brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: fzf --preview shows the file under the cursor while you pick

Fzf
~/tips/tip-fzf-preview-window

fzf --preview 'cat {}'

Adds a preview pane to fzf that shows what the highlighted line points to.

I like fuzzy finders for the typing and dislike them for the guessing. A list of file names tells me little when three of them are called config. A preview pane fixes that.

fzf --preview 'cat {}'

According to the fzf README, with --preview set, fzf starts an external process with the current line as the argument. {} is replaced with the single-quoted string of the focused line, and the output of your command appears next to the list. Without a pipe on stdin, fzf walks the file system under the current folder to build the list of files, so this one command is a file picker with a viewer.

If you have bat, the README pairs the preview with it, because the preview supports ANSI colours:

fzf --preview 'bat --color=always {}' --preview-window '~3'

--preview-window sets the size, position and border of the pane. The README example uses the value ~3.

The same idea works on any list you pipe in. A list of process lines or branch names can have a preview command that looks up details for that line.

fzf writes the selected item to stdout, which means you can wrap it, for example in a $(...) that hands the chosen path to another command.

This was not run against a live fzf. It follows the README and the options listed there.

One caveat: the preview command runs for every line you move over, so a slow command makes the list feel slow. Keep the preview cheap, and do not point it at a command that writes or deletes anything, because it will run just by moving the cursor.

Further reading: fzf README

Going deeper: Run degoog on Unraid as your own search page, compared with SearXNG and 4get

More Fzf tips eval "$(fzf --bash)" All Fzf tips (2) All topics

Link