19

When searching using:

C-s SPC SPC

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

23

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.

  • Thanks for this. Might be good to actually put the .emacs line (set-variable 'search-whitespace-regexp nil) in there. Thanks. – Gray Mar 28 '18 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 '18 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 '18 at 0:19
3

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
  • 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 – Jonas Dahlbæk Mar 16 '17 at 19:57
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.

  • 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 '18 at 7:49

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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