In Emacs how can I easily copy all lines matching a particular regex? Preferably highlighting the matching lines as I type.
occur
gets partway there by
As of Emacs 24, occur
does in fact provide a simple solution:
C-uM-so .*pattern.*
RET
When you use C-u on its own as the prefix argument, the matching portion of each line is inserted into the *Occur*
buffer, without all the normal adornments.
Note that because only the part of the line matching the regexp is used (unlike a normal occur), you need the leading and trailing .*
to ensure that you capture the entire line.
The details of how occur
treats arguments are a little tricky, so read C-hf occur
RET carefully if you want to know more.