23

Inspired by modern browsers, I'd love to be able to see how many occurrences there are of some string, when I search for it – like the "10 of 37" at the top-right corner of the screenshot below.

Chrome 10 of 37

Is there a way of getting this in Emacs? Presumably, so as to not be slow, it should run only after some idle time.

5
  • This is a duplicate of these [emacs]-tagged questions on StackOverflow: stackoverflow.com/q/14764130/729907, stackoverflow.com/q/22479140/729907. Apparently it cannot be closed as a duplicate, so I guess that means that folks can copy the zillions of other SO [emacs]-tagged questions... Bonjour les degats.
    – Drew
    Commented Oct 10, 2014 at 4:00
  • @Drew: Thanks for those pointers. Obviously it's not reasonable to consider questions as duplicates across different websites, any more than you'd consider a question here a "duplicate" of something asked somewhere else (e.g. on gnu.emacs.help). Yes the hope for this site is that if good questions from SO are reproduced here, it should improve the quality of this site. I didn't know the question had been asked on SO, but it's definitely a good idea for someone with the time to start asking (and answering) the best questions here. Maybe there should be a way to migrate questions automatically. Commented Oct 10, 2014 at 5:26
  • Why? Why is it a good thing to reproduce the "good questions from SO" here? What is the aim? This Beta tests whether it makes sense to have a separate Emacs site, as opposed to using tag [emacs] on SO etc. If we "prove" that this site is meaningful by just copying questions from elsewhere, then what have we shown? This makes no sense to me. And why would we want to encourage such behavior? Who does it help (besides the copier)? If we wanted to migrate stuff (which we do not, during Beta) then at least the original questioner and respondents would be kept in the loop.
    – Drew
    Commented Oct 10, 2014 at 15:00
  • @Drew: The aim is to have a good place for asking and answering questions about Emacs. All SE sites are encouraged to "seed" the site during (private) beta with good questions and answers, so that later visitors to the site know what kind of content is appropriate. Anyway, I only came here because I heard of this site and thought of asking the questions that were bothering me at the moment; those thoughts about migrating questions were spurred by your remarks in the first comment above. Commented Oct 11, 2014 at 3:41
  • OK. Please take it to Meta, if you have something more to say.
    – Drew
    Commented Oct 11, 2014 at 5:55

6 Answers 6

15

The anzu package does that.

anzu.el provides a minor mode which displays current match and total matches information in the mode-line in various search modes.

screenshot

1
  • Thanks! I added MELPA and installed anzu and turned it on, and it seems to be working. I'll wait to see if anyone else has a solution that doesn't depend on any packages (at least, packages not in ELPA). Commented Oct 9, 2014 at 23:13
14

Here are some possibilities that aren't very slick, that have the advantage of working with a stock Emacs.

If you press M-s o (isearch-occur) during an incremental search, an Occur buffer pops up with the current search expression. At the top of the *Occur* buffer is the number of matching lines.

The command how-many displays the number of occurrences of a regexp (including repeated occurrences). Unfortunately it isn't integrated with incremental search. Here's a proof-of-concept isearch integration: press M-s # during isearch to show the number of matches.

(defun isearch-how-many (regexp)
  "Run `how-many' using the last search string as the regexp.
Interactively, REGEXP is constructed as with `isearch-occur'."
  (interactive
   (list (cond
      ((functionp isearch-word)
       (funcall isearch-word isearch-string))
      (isearch-word (word-search-regexp isearch-string))
      (isearch-regexp isearch-string)
      (t (regexp-quote isearch-string)))))
  (how-many regexp nil nil (interactive-p)))
(define-key isearch-mode-map [?\M-s ?#] 'isearch-how-many)
2
  • This... yeah. It's not exactly what I want, but it's valuable to know the best that's possible with stock Emacs, thanks. Commented Oct 10, 2014 at 0:03
  • This seems only showing the number of matching lines, not number of matches.
    – xuhdev
    Commented Apr 25, 2017 at 18:03
11

As of emacs 27.1, this is now built-in to isearch.

To turn on:

    (setq isearch-lazy-count t)

To compare with anzu, the count is shows in the minibuffer prompt, rather than the modeline.

See the manual for details.

2
  • That's good to hear, thank you! I was wondering whether it also highlights the occurrences of matches in the scrollbar, as browsers and some other text editors do, but apparently not yet. But that's a topic for a separate question… Commented Mar 1, 2021 at 6:14
  • This should be the accepted answer Commented Mar 25, 2021 at 22:25
3

I would like to suggest Swiper!

Swiper is an alternative to isearch that uses ivy to show an overview of all matches.

enter image description here

1
  • Thanks, looking good! I have Swiper set-up, but it doesn't show the (total) number of instances, as in your screenshot. Anything I have to include in my init file to enable this or is this no longer available in recent versions? I appreciate it's been a few years since you posted this, so things may have changed ,,.
    – Tiberius
    Commented May 27 at 17:18
2

One possibility that requires no package at all is M-x count-matches (or, similarly, and also very expressive, M-x how-many).

It works with both strings and regular expressions!

1
  • Shoud be considered as the best solution Commented Oct 18, 2022 at 2:07
0

As mentioned in bnzmnzhnz answer, set

(setq isearch-lazy-count t)

But for this to take effect you also need

(setq isearch-lazy-highlight t)

You can see that this is documented if you do Ctrl-h-v for isearch-lazy-count

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.