2

Typically, at the beginning of a coding/maintenance task, I usually start with a carefully worded grep, such as

M-x grep
git --no-pager grep -n -e 'collect' -- ':!*spec*' ':!*.yml'<enter>

and suppose I got 200 grep results. Usually a task requires changing only 1 or 2 files, but figuring out which files that needs the edit is the hard part in the beginning. so I would essentially "eliminate suspects" from the grep results, until I come up with a "manageable" list of files I can focus on/dig in to.

How can I effectively work with the grep results in emacs, do we have the ability to "eliminate" lines interactively ?

1
  • One idea would be to create a custom deletion function that uses separator lines (e.g., when using context lines) between matches, and then use those separate lines to help Emacs programmatically identify the beginning/ending of each matched section -- deleting an entire section with the buffer being writeable for the duration of the deletion function. If there are no separator lines, then just deleting the line as NickD suggested in the answer below is the easiest.
    – lawlist
    Jul 27, 2020 at 1:56

3 Answers 3

6

You can always make the *grep* buffer writable with C-x C-q and delete lines the normal way (e.g. with C-a C-k C-k). When you are done, press C-x C-q again to make it read-only if you wish and use TAB and ENTER to navigate and visit files in the list.

1
2

Just to add to what @NickD said, which is what I too recommend:

After making buffer *grep* writable, use commands flush-lines and keep-lines to quickly remove lines you're uninterested in.

  • M-x flush-lines - Remove all lines after point that match a regexp you provide.

  • M-x keep-lines - Remove all lines after point that do not match a regexp you provide.

This can save a lot of time visually checking lines and removing them by hand.

You can obviously use these commands multiple times, with simple regexps, to get the effect of filtering out multiple things. That's typically much easier and quicker than trying to come up with complex regexps that filter multiple things at the same time.


Note that the same thing applies to other listings of search hits and other things, in particular the output of M-x occur.

0

I can recommend double-saber mode - https://github.com/dp12/double-saber for further filtering of grep results.

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.