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
Emmanuel Goldstein
  • 1,014
  • 8
  • 18
  • 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 Sep 07 '22 at 13:31
  • If you add your response as a proper response, I will accept it. Thanks! – Emmanuel Goldstein Sep 13 '22 at 13:19

1 Answers1

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.

...

NickD
  • 29,717
  • 3
  • 27
  • 44