27

When searching using:

C-s SPC SPC

Also matches single spaces. I want to match exactly two space.

1
  • The thing is that it violates the principle of least surprise IMHO. In the past I always preferred Emacs, because it did what I wanted, not what Emacs wanted ("beware of artificial intelligence").
    – U. Windl
    Mar 31 at 8:04

3 Answers 3

31

Use M-s SPC during Isearch to toggle matching whitespace literally. When matching literally, each SPC char you type is matched individually. (This used to be the default Emacs behavior, BTW.)

To configure this as the default behavior customize option search-whitespace-regexp to nil. (M-x customize-option search-whitespace-regexp.)

See the GNU Emacs manual, node Special Isearch.

C-h v search-whitespace-regexp tells us this (showing the default value):

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.

This variable was introduced, or its default value was changed, in version 24.3 of Emacs.

5
  • Thanks for this. Might be good to actually put the .emacs line (set-variable 'search-whitespace-regexp nil) in there. Thanks.
    – Gray
    Mar 28, 2018 at 13:49
  • 1
    @Gray: set-variable doesn't save the new value persistently, so that won't change the default behavior. I think M-x customize-option is what is called for here.
    – Drew
    Mar 28, 2018 at 23:57
  • Oh I was talking about it in the dot emacs file. Didn't know about customize option. Thanks for editing.
    – Gray
    Mar 29, 2018 at 0:19
  • @Drew The answer could benefit if you'd explain not just how to fix it, but also explain what is happening by default. Also I think the behavior was different in the past (being an old-time Emacs user). The "feature" seems to be new since Emacs 25.1 (search-default-mode, isearch-regexp-function)
    – U. Windl
    Mar 31 at 8:17
  • @U.Windl: Done. (It's apparently Emacs 24.3, BTW.)
    – Drew
    Mar 31 at 18:46
4

Using regexp incremental search solves the problem, if you escape the space characters:

C-M-s \SPC\SPC

You can also use search and replace without the backslashes, if that's what you're after:

M-% SPC SPC
3
  • I ran into the same problem today when building a keyboard macro. This answer solved the problem for me, though I had to use the key combination ESC C-s instead. See also github.com/leoliu/ggtags/issues/64 Mar 16, 2017 at 19:57
  • Either I don't understand the meaning of \SPC, or it doesn't work for me (Emacs 24.3.1):
    – U. Windl
    Mar 31 at 8:21
  • Use a backslash character followed by a space character.
    – Rovanion
    Mar 31 at 8:37
3

Using regexp search helps, but you can also make sure of literal spaces for the search using C-q SPC to quote a space into the search.

To riff off of @Rovanion's answer then, using isearch-forward-regexp:

C-M-s C-q SPC C-q SPC

That will search for two consecutive spaces. Interestingly, isearch-forward-regexp requires discrete matches. Searching aaa for aa will only match once and not again at the second character.

2
  • Regarding your last note, imagine searching for "zero or more whitespace followed by a thing" and, given N characters of leading whitespace, needing to step through all N+1 individual matches. I can see that being a reasonable option to provide, but it would be a terrible default.
    – phils
    Dec 22, 2018 at 7:49
  • Actually whether using a plain space character (SPC) or qoting it in incremental search does not make a difference (in Emacs 24.3.1); it only works for C-M-s, but not for C-s. Maybe improve the answer to explain in greater detail.
    – U. Windl
    Mar 31 at 8:23

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.