When searching using:
C-s SPC SPC
Also matches single spaces. I want to match exactly two space.
Emacs Stack Exchange is a question and answer site for those using, extending or developing Emacs. It only takes a minute to sign up.
Sign up to join this communityWhen searching using:
C-s SPC SPC
Also matches single spaces. I want to match exactly two space.
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 inisearch.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
, orisearch-lax-whitespace
isnil
for ordinary incremental search, orisearch-regexp-lax-whitespace
isnil
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.
(set-variable 'search-whitespace-regexp nil)
in there. Thanks.
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.
search-default-mode
, isearch-regexp-function
)
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
ESC C-s
instead. See also github.com/leoliu/ggtags/issues/64
Mar 16, 2017 at 19:57
\SPC
, or it doesn't work for me (Emacs 24.3.1):
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.
C-M-s
, but not for C-s
. Maybe improve the answer to explain in greater detail.