11

Is it possible to make isearch disregard line breaks, i.e. a search for foo bar will find

.......... foo
bar .....

I get bitten by isearch failing on such queries regularly when navigating long documents, and it's rather annoying. I am aware that word-search-forward finds matches across lines, but it has certain shortcomings (e.g. it forces you to use complete words, so a search for "foo b" will fail; it doesn't highlight search results), and, more importantly, it's a little silly to use a different command depending on whether you are searching for one word or two.

2
  • It might be useful in many cases to ensure line breaks are cosmetic in Emacs. Use global-visual-line-mode to wrap lines without affecting buffer contents. If there is an actual carriage return across lines, well, that seems like it should be something you would have to search for, since it is a paragraph break or a new statement in the program code, for example. May 20, 2011 at 5:24
  • 2
    It might be useful, however it's hardly standard practice. In particular, I have never come across a LaTeX document without hard CRs. May I remind you that both global-visual-line-mode and longlines-mode which preceded it are fairly young by emacs standards? Lots of legacy documents to deal with. May 20, 2011 at 5:36

3 Answers 3

6

Use C-M-s - isearch-forward-regexp. This will interpret space characters as a marker for any white space character.

3
  • 3
    This works. Maybe I'll start using regexp isearch instead of regular isearch then... Note that you also need to customize search-whitespace-regexp (see C-h v search-whitespace-regexp for details). May 20, 2011 at 5:46
  • 2
    I find that I don't need to use isearch-forward-regexp in order to obtain the desired behaviour once I customise search-whitespace-regexp to something like [ \t\r\n]+ as suggested in the documentation. It is enough to use search-forward as usual. Dec 17, 2013 at 9:44
  • @JoséFigueroa-O'Farrill Please add an answer with this information.
    – Teddy
    Jan 9, 2014 at 16:33
4

As prompted by @Teddy, here’s my comment in answer form.

In order to obtain the desired behaviour, I did not have to customise isearch-forward-regexp, but simply customise search-whitespace-regexp to [ \t\r\n]+as suggested in the documentation. This way you can then use search-forward as usual.

1
1

In a recent enough emacs, isearch-forward-word, which is bound to M-s w, is a simpler solution: there is no need for any configuration of search-whitespace-regexp.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.