5

I am trying to line up a text table using align-regexp. A table, before alignment, looks like this:

| Alpha | Bravo | Charlie |
|-------|-------|---------|
| Delta | Echo | Foxtrot |
| Golf | Hotel | India |

and after alignment should look like this:

| Alpha | Bravo | Charlie |
|-------|-------|---------|
| Delta | Echo  | Foxtrot |
| Golf  | Hotel | India   |

The best result I've gotten is C-u M-x align-regexp, then \(\s-*\)| as regexp, then repeat throughout line. What I get is this:

                        | Alpha  | Bravo  | Charlie  |
                        |------- |------- |--------- |
                        | Delta  | Echo   | Foxtrot  |
                        | Golf   | Hotel  | India    |

The pipes line up, but now it's indented some random number of spaces that seems to be dependent on the content of the table. The problem with this is that markdown, the format of the document, now treats it as code because its indented.

My quick, but tedious, fix is to un-indent it, but I'd like to find out the right incantation for align-regexp to make this indentation work properly.

rityzmon
  • 181
  • 3
  • I'd use the regex `\(\)|`, the group 1 and a spacing (i.e. default amount of padding) of 0 repeated throughout the line. – mutbuerger Jul 09 '16 at 15:06

1 Answers1

5

I wouldn't use align-regexp but the wonderful orgtbl-mode for this: if you enable this minor mode, it will automatically recognize the table, and change its size as needed (use tab to move around in the table).

Rémi
  • 1,607
  • 11
  • 13