An Improved Emacs Search

May 2023

Emacs's incremental search is very powerful. Rather than wait to show results until the search query is completely typed, it immediately begins searching once the first character is typed, updating the search as more letters are pressed. This makes it very responsive, and provides answers immediately.

But not everything is great about it.

By default, the incremental search doesn't provide any context. How many matches are there? Is this result near the beginning or the end?

incremental search, as default

Computers are better than humans at counting. Let's make Emacs count the matches for us.

(setq isearch-lazy-count t)

Enabled lazy counting!

This feature is called lazy counting. I'm not sure if it's calling us lazy for using it, or saying that Emacs will lazily count the matches.

When enabled, Emacs displays the number of search results in the minibuffer, along with the index of the current search result. It's very useful to add context.

However, the default display is difficult to read. We can change that too.

(setq lazy-count-prefix-format nil)
(setq lazy-count-suffix-format "   (%s/%s)")

This puts the counts at the end of the search message, not the start.

A nice experience!

Now that's a feature that counts!

If you want to hear when I publish more, sign up for my mailing list!

    previous

    < Nix Development Shells
    tag: emacs

    next

    Don't make Emacs keymaps -- define them. >

    previous

    < Nix Development Shells
    tag: til

    next

    Don't make Emacs keymaps -- define them. >