Ads by Google

Sunday, June 26, 2011

A simpler search: Emacs Occur

The Emacs manual has a section devoted to search and replacement exploring a multitude of options to search and replace text.  You should at least be familiar with
`C-s'
     Incremental search forward (`isearch-forward').
`C-r'
     Incremental search backward (`isearch-backward').
Anything a bit more complicated than that, you need to dive into the manual.

And if you really do read the section end to end, you'd see the last part on Other Repeating Search.

And you'll find another simple search M-x occur. Call it, supply the search string and that's it.  It provides all the matches in another buffer, each line clickable to take you to the specific line  in the source file or buffer where the match was made.  It is also mapped to M-x list-matching-lines.  And if you want to search multiple buffers, there's M-x multi-occur and M-x multi-occur-in-matching-buffers, the latter that takes a regular expression for file names.

And if you do want a bit of context to the matches, supply a prefix argument and it will show contextual lines to the matches; i.e C-u 2 M-x occur searchtext RET will show 2 lines of context with every match. 

That's it. M-x occur is for the occasions where you can't be arsed to find the 100% correct regex and no one's watching you hacker FAIL.

And you should really read the complete section on Search and Replace.  It's well worth your time.
Mastering Regular ExpressionsRegular Expressions CookbookBeginning Regular Expressions (Programmer to Programmer)