LinkedIn Sourceforge Twitter

Vincent's Blog

Pleasure in the job puts perfection in the work (Aristote)

Fuzzy search within the OpenBSD packages

Posted on 2020-05-24 22:24:00 from Vincent in OpenBSD

fzf is an addicting tool for what concerns search. The overall process is very simple, despite very quick. Moreover the tool is well presented. This post will describe how to integrate fzf with OpenBSD packages.


Searching keyword in packages description or package name is not the most powerful feature we have in OpenBSD. The FAQ proposes to search a file or a keyword in the package's name (by doing a grep in the ports tree). Or to perform SQL queries for advances search.
For simple search in package's name, we have pkg_info -Q to our help.

When I saw what others are doing with fzf, like apt-fzf, I had the idea to perform the same with OpenBSD.


You first have to install some packages:

doas pkg_add fzf sqlports

fzf is the fuzzy search tool.

sqlports is an SQLite DB consolidating all information concerning available packages.

Fuzzy search combined with pkg_add

Basically the idea is to provide to fzf the result of a SQLite query. Then the output of fzf will be send to pkg_add.

In order to have some package's details in the right panel, we will have an extra script for that only purpose.

Thus, if you count well, we will have 3 small scripts: pkg_search, fzpkg_add and pkg_descr


This script will search in SQLite DB build by sqlports all packages containing the keyword provided as parameter either in the package's name or in the package's comment or in the package's description.



The core script is the one feeding fzf and analysing the outputs of fzf in order to send them to pkg_add.

pkgs=$(pkg_search $1 | fzf-tmux -m --tac +s --preview "pkg_descr {1}" --bind "ctrl-d:toggle-preview")
set -A params
for line in $pkgs
  pkg=$(echo $line | cut -d"|" -f2)

[ -n "$params" ] && (echo "pkg_add ${params[@]}" ; pkg_add ${params[@]})

The exchange of information between the scripts is always based on the packages's PathId. This is a unique identifier inside sqlports for each packages.

As you can also see, the right panel can be hidden by hitting Ctrl-d.

To keep an sort based on those PathID, which follows more or less the package's name, we ask fzf to not sort data.

The script is able to manage multiple selections. All selected packages will be sent to the command pkg_add


Finally a small script displaying package's details.

pkgid=$(echo $1 | cut -d "|" -f1)
echo "Category:"
sqlite3 /usr/local/share/sqlports "select DISTINCT FullPkgPath from Categories where PathId="$pkgid";"
echo " "

res=$(sqlite3 /usr/local/share/sqlports "select Value from Flavors where PathId="$pkgid";"| tr '\n' ', ')
[ -n "$res" ] && echo "Flavors:\n${res%%,}\n"

echo "Description:"
sqlite3 /usr/local/share/sqlports "SELECT Value FROM Descr WHERE PathId = "$pkgid";"

In this script we look for most relevant details of a package: his category, if he has flavors and a package's description.

Download source files

You can download all those scripts from my fzpkg_add repository


A small video showing how it works:(better in full screen)


All in all those small scripts around sqlports and fzf are really useful if you want to search for packages with having /usr/ports installed and without querying

This match perfectly my own needs. Do it is useful for other OpenBSD users ?
Should develop further those scripts ?

46, 45
displayed: 13359

1. From Marc on Thu Nov 12 16:12:14 2020

very nice, pkg tools are kind of short of utility when you compare them to freebsd and this scripts help quite a bit. Thank you!

2. From Marc on Thu Nov 12 17:09:26 2020

a couple of suggestions/feedback: - it would be nice to use a modifier to decide if you want to search only name, full name or/and description. sometimes it can bring a lot of extra output, for example "fzpkg_add kde" show a lot of unrelated packages. - i'd rather not use fzpkg_add as root, so it would be great that it used doas/sudo and asked for confirmation before actually using pkg_add at the end of the script to avoid installing packages by error. great job!

3. From Vincent on Fri Nov 13 00:20:41 2020

Thanks Marc for your support. Concerning suggestions, I'll look at them. In any cases feel free to provide patches.

What is the last letter of the word Python?