0

Basically that: would like to find an easy way to convert all those consecutive blank spaces into one in Emacs. Thanks for your help.

This:

text text  text  text       text text  text

Into this:

text text text text text text text
2
  • 2
    It depends on what you mean by an "inbuilt" function: does replace-regexp count? (replace-regexp " +" " " nil (point-min) (point-max)) should do that. The regexp matches two or more spaces and the replacement is a single space.
    – NickD
    Commented Sep 7, 2022 at 13:31
  • If you add your response as a proper response, I will accept it. Thanks! Commented Sep 13, 2022 at 13:19

1 Answer 1

2

[Comment made into an answer]

(replace-regexp " +" " " nil (point-min) (point-max)) should do that. The regexp matches two or more spaces and the replacement is a single space. point-min and point-max return the positions of the (visible) portion of the buffer, so they establish the whole (visible) buffer as the region to apply the replacement to - you can season to taste.

As usual, read the doc string of the function in its entirety with C-h f replace-regexp for the details (the following is an edited version to highlight details relevant to the question):

(replace-regexp REGEXP TO-STRING &optional DELIMITED START END
BACKWARD REGION-NONCONTIGUOUS-P)

Replace things after point matching REGEXP with TO-STRING.
Preserve case in each match if ‘case-replace’ and ‘case-fold-search’
are non-nil and REGEXP has no uppercase letters.


...

In Transient Mark mode, if the mark is active, operate on the contents
of the region.  Otherwise, operate from point to the end of the buffer’s
accessible portion.

...

Fourth and fifth arg START and END specify the region to operate on.

...

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.