isearch: semi-fuzzy search
Let’s kick this blog off with a simple yet handy isearch hack that I’m pretty sure I originally lifted from prot’s Emacs configuration:
(defun my/isearch-fuzzy-forward (&optional regexp-p)
"Fuzzy version of `isearch-forward'."
(interactive "P")
(let ((search-whitespace-regexp ".*?"))
(isearch-forward regexp-p)))
(defun my/isearch-fuzzy-backward (&optional regexp-p)
"Fuzzy version of `isearch-backward'."
(interactive "P")
(let ((search-whitespace-regexp ".*?"))
(isearch-backward regexp-p)))
The idea is simple: set search-whitespace-regexp such that any
sequence of characters can count as whitespace. This means as long as
the individual search terms in your query are found on the same line,
it doesn’t matter what’s between them.
The terms still must match in order, so it isn’t quite true fuzzy search. But for a trivial amount of code and no added packages, it works great.
This can be set permanently in your config. I personally like it better applied via dedicated functions. I use isearch as part of my core editing flow to jump around the text on screen, and for that I still want literal matching.
I bind these functions to C-S-s and C-S-r respectively.