I have following file:


I would like to perform 'narrowing search', so if I search for aaaa my original buffer will become:


Is there any tool for doing such thing ?


The loccur package, also available on Melpa, does exactly what you want.

From its github page:

loccur is a minor mode for Emacs acting like occur but without creating a new window. It just hides all the text except lines containing matches for a given regexp.

  • nice, I see it uses invisible overlays to achieve the effect. – stsquad May 29 '15 at 8:53

So you can do occur or use something fancier like helm-swoop to bring up a narrowed search buffer depending on your criteria. Both of these modes can then edit the search buffer and the results can be saved to the original buffer. The narrow functions are really just for contiguous lines of text (by region, defun or page).

  • So you suggest some workaround. Is it technically possible to write dedicated mode anyway ? – Scony May 28 '15 at 9:36
  • @Scony it might be possible to achieve the same effect in-buffer using the same mechanism as the narrow functions but I haven't looked at the code closely. – stsquad May 28 '15 at 9:45
  • @Scony: OK I looked at the code and I don't think the narrow code could do it. It's implemented in the C core and all it does is set the start/end pos of the buffer so by definition it has to be a contiguous region. The only other option I can think of is using something like an overlay to hide non-matching text but I suspect that would be hacky. – stsquad May 28 '15 at 9:52
  • @Scony: this might help you get started gist.github.com/stsquad/39d84d81870a11bc1a21 – stsquad May 28 '15 at 10:21
  • occur and helm swoop dont come close to meeting the criteria. Both use other buffers. – RichieHH Dec 8 '20 at 21:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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