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.
occur
andloccur