I've only recently started using emacs, for professional reasons. I'm trying to replace all double spaces in a text file by single ones using regex. In C-M-s the expression \s-\{2\} matches two spaces all right but also more which I don't want. And in a search and replace query the same expression doesn't match anything.


What you want to use interactively is this: \s-\{2\}, not this: \s-\\{2\\}.

That works for me, for both C-M-s (isearch-forward-regexp) and C-M-% (query-replace-regexp).

You edited your question, changing \s-\\{2\\} to \s-\{2\}. But I don't see a problem with the latter. It does not match more than two consecutive space chars, for me. And it does match two for query-replace also, for me.

(It can be confusing to go back and forth between entering a regexp interactively and using a regexp in Lisp code. Interactively you typically do not use \\.)

| improve this answer | |
  • Sorry, I made a mystake in my post. I actually used \s-\{2\} – prof Mar 30 '19 at 17:14
  • Then I think you'll need to give a more detailed recipe to repro the problem - step-by-step, starting from emacs -Q. I don't see a problem when I do that. – Drew Mar 30 '19 at 17:15
  • It works when I do C-x h and then add the regex. So I guess it did not work because I was not selecting the whole text. Is that it? – prof Mar 30 '19 at 17:50
  • You should not need to select any text. It's not clear to me what you're trying. Please edit the question to provide a step-by-step recipe of what you're doing - what keys you hit etc. You should be able to just use C-M-s or C-M-% directly, providing the regexp \s-\{2\}. (For the replacement text for C-M-% just type a single space char.) – Drew Mar 30 '19 at 17:59
  • I am bit confused as well. I am new at this. But if I put the cursor at the beginning of the buffer or before the pattern I'm looking for, it works. Thank you for your help. – prof Mar 30 '19 at 18:15

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.