4

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.

  • 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 9 '16 at 15:06
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).

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.