11

I am used to writing files where paragraphs are split into several lines, e.g. with fill-column. When I search for a string (C-s) such as this is a long text, the search results would not return the locations where the string appears with a line break between two of the words (like this\nis a long text).

Is there a command that would also return these results?

11

Take a look at the variables isearch-lax-whitespace, isearch-regexp-lax-whitespace, and search-whitespace-regexp.

If the first two variables are set to something non-nil (e.g. t) any space character in your search string will match any sequence matched by the regular expression defined by the search-whitespace-regexp variable.

To match words across line breaks do this:

(setq isearch-lax-whitespace t)
(setq isearch-regexp-lax-whitespace t)
(setq search-whitespace-regexp "[ \t\r\n]+")

A space character in your query will now match any space, tab, or linebreak any number of times.

  • Thanks, but that does not work for me. My Emacs version is 23.4.1. Apparently it does not have an "isearch-lax-whitespace" variable. – Tony Apr 9 '15 at 11:57
  • 1
    Yeah, these features were introduced in Emacs 24.3. Any chance of upgrading to a more recent version? Version 23.4.1 is already more than three years old. – rekado Apr 9 '15 at 12:47
  • 2
    Try isearch-forward-word, which is bound to M-s w in recent Emacs. – politza Apr 9 '15 at 20:36
  • Emacs 23 didn't work for me, make sure you're using 24. – ragerdl Jan 29 '16 at 23:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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