2

Suppose I have a text document (e.g. org-mode, Markdown, etc.) in which I would like to search for the phrase "hello world". Unfortunately, if paragraphs in this document are filled (e.g. via auto-fill-mode), then there might be a newline between "hello" and "world". Is there any way I can have isearch treat any whitespace, including line breaks, as equivalent to a single space when searching? I know I can isearch-forward-regexp for hello[[:space:]]+world, but that's a little more difficult to remember and awkward to type for something that I'd rather have as the default behavior.

1 Answer 1

2

Option search-whitespace-regexp:

search-whitespace-regexp is a variable defined in isearch.el.

Its value is "\\s-+"

Documentation:

If non-nil, regular expression to match a sequence of whitespace chars.

When you enter a space or spaces in the incremental search, it will match any sequence matched by this regexp. As an exception, spaces are treated normally in regexp incremental search if they occur in a regexp construct like [...] or *, + or ?.

If the value is a string, it applies to both ordinary and regexp incremental search. If the value is nil, or isearch-lax-whitespace is nil for ordinary incremental search, or isearch-regexp-lax-whitespace is nil for regexp incremental search, then each space you type matches literally, against one space.

You might want to use something like "[ \t\r\n]+" instead.

In the Customization buffer, that is [ followed by a space, a tab, a carriage return (control-M), a newline, and ]+.

You can customize this variable.

See the part in bold, which tells it to match also newline chars (\n).

See also M-s SPC (isearch-toggle-lax-whitespace) during Isearch:

isearch-toggle-lax-whitespace is an interactive compiled Lisp function in isearch.el.

(isearch-toggle-lax-whitespace)

Toggle lax-whitespace searching on or off.

In ordinary search, toggles the value of the variable isearch-lax-whitespace. In regexp search, toggles the value of the variable isearch-regexp-lax-whitespace.

You can also just search explicitly for a match that allows newline chars: C-M-s hello[ SPC C-j TAB ]+world. (Interactively, use C-j to insert a newline char.)

Your Answer

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

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