I want to filter the text of an emacs buffer, a bit like you can filter text with grep.

Sounds simple, but I tried googling it and I only get results from people who want to search in a buffer or to grep the filesystem.

That is not what I want. I want to filter the text of a buffer. I want the text not matching the pattern to be gone and I don't want to grep the files in my filesystem.

Is this possible? I'm currently saving the output of the buffer to a file and doing this in a shell:

more file.txt | grep pattern

I want the text not matching the pattern to be gone

  • M-x keep-lines will delete lines not matching the pattern (i.e. only keep the lines which match). flush-lines does the opposite, deleting lines which do match the pattern.
  • M-x occur will open a separate buffer showing only the matching lines (which you can use like a compilation error buffer to navigate those positions in the original buffer). This option is the closest to the 'grep' approach.
  • C-uM-x occur will show only the portion of each line which specifically matched the pattern (which is what the quoted part of your question sounds most like).
  • M-x loccur from https://elpa.gnu.org/packages/loccur.html hides/unhides the text within the original buffer (which is therefore a bit like a non-destructive keep-lines).

You can read about those built-in commands, and others, in the manual:
C-hig (emacs)Other Repeating Search

Also see (emacs)Compilation Mode regarding the convenient navigation using the next-error and previous-error commands.

Finally, note that if you do need to use grep itself, you are better off doing that inside Emacs as well (not least because you can once again jump from the results to the original locations like in a compilation buffer). There are many Emacs commands which use grep, and you can read about those at:
C-hig (emacs)Grep Searching

Tangentially, I'll recommend the third-party grep equivalent to Occur Edit mode, which you can find at https://melpa.org/#/wgrep.

  • Amazing! Is there also any way to use sed | sort | uniq as well? Maybe I'm asking too much, but just in case.
    – Romário
    Sep 23 at 22:34
  • 1
    @dalanicolai's answer has you sorted there.
    – phils
    Sep 23 at 22:40
  • You can also use sort-lines and delete-duplicate-lines (use C-u C-u to properly mimic uniq's adjacent line requirement)
    – Felipe
    Oct 13 at 16:41

Besides the options mentioned in phils answer, you can use shell-command-on-region for this. Select the region you'd like to 'filter' first. When prefixed with a universal argument C-u, the sent text gets replaced (you can read its docstring for more info).

I don't know your exact usecase, but generally swiper or helm-swoop provide similar and generally more useful functionality.

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.