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 coping them into a buffer, but it adds lots of extra stuff.
Once you have a region selected, the most basic commands are: To cut the text, press C-w . To copy the text, press M-w . To paste the text, press C-y .
C-x h will select the entire buffer. You can search for help within Emacs using the built-in help system.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With