Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs copy matching lines

Tags:

emacs

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.

like image 672
Singletoned Avatar asked Feb 18 '10 15:02

Singletoned


People also ask

How do you copy lines in Emacs?

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 .

How do I select all in Emacs?

C-x h will select the entire buffer. You can search for help within Emacs using the built-in help system.


1 Answers

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.

like image 169
phils Avatar answered Oct 01 '22 08:10

phils