Search code examples
emacsrectangles

Emacs rectangle space removal


I have something like the following data set

A, B  ,C,D ,  E 
A1 , B121  ,C1,D1 ,  E1 
A2,Ber2  ,C2,D2 ,  E2 
A3, Bat3  ,C3,D3 ,  E3 

And I want the commas to align so that they are right after the text ends and there is a space after which the next column starts. Like this

A, B, C, D, E 
A1, B121, C1, D1, E1 
A2, Ber2, C2, D2, E2 
A3, Bat3, C3, D3, E3 

I tried using delete-whitepsace-rectangle but that only works as long as the width of the strings ina column match for some reason . Is there a way to make this happen in emacs?


Solution

  • You want to replace spaces, a comma, and spaces, with a comma and a single space.

    You can do this with replace-regexp, replacing "\ *,\ *" with ",".