I have a text with a number of coherent strings, e.g.:


Is there a quick way to convert these so that there be a space between each of them, i.e.:

a x 9 2 3 f a s d
a s i d n a 1
x x a 0 0 8
  • 1
    Is your text in a buffer, or is it a string object?
    – phils
    Mar 11 '18 at 19:44
  • If this is text in a buffer, do you want to do this interactively (via command) or in Lisp code?
    – Drew
    Mar 11 '18 at 22:42
  • text is in a buffer; would like to do it interactively; phils answer down below solved it nicely.
    – miri sueß
    Mar 12 '18 at 19:54

There are lots of ways you could do that.

Assuming this is text in a buffer, a simple regexp-based approach is to use M-x replace-regexp to replace . with \& (n.b. that's \&SPC) to replace each non-newline character with itself followed by a space.

As that does match the final character of each non-blank line, that will introduce a space at the end of those lines, which you might wish to clean up with delete-trailing-whitespace or similar.

A fancier interactive regexp replacement which doesn't append the trailing space would be:

\,(concat \& (if (eolp) "" " "))

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.