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.

ShreevatsaR
  • 880
  • 6
  • 19
  • This is a duplicate of these [emacs]-tagged questions on StackOverflow: http://stackoverflow.com/q/14764130/729907, http://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 Oct 10 '14 at 04: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. – ShreevatsaR Oct 10 '14 at 05: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 Oct 10 '14 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. – ShreevatsaR Oct 11 '14 at 03:41
  • OK. Please take it to [Meta](http://meta.emacs.stackexchange.com/q/139/105), if you have something more to say. – Drew Oct 11 '14 at 05:55

5 Answers5

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

Wilfred Hughes
  • 6,890
  • 2
  • 29
  • 59
Kaushal Modi
  • 25,203
  • 3
  • 74
  • 179
  • 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). – ShreevatsaR Oct 09 '14 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)
8

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.

Muihlinn
  • 2,576
  • 1
  • 14
  • 22
bnzmnzhnz
  • 81
  • 1
  • 1
  • 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… – ShreevatsaR Mar 01 '21 at 06:14
  • This should be the accepted answer – Jérémy Pouyet Mar 25 '21 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

serghei
  • 272
  • 3
  • 15
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!

kotchwane
  • 481
  • 2
  • 12