Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Align to columns in emacs?

I have a .dat file which looks like this:

NGC0448 A3D:2010B g:o,r:o,i:o
NGC0474 A3D:2011A,2013A g:o,r:o,i:o,u:o

Now I want to align them to be in this form:

NGC0448    A3D:2010B          g:o,r:o,i:o
NGC0474    A3D:2011A,2013A    g:o,r:o,i:o,u:o

I tried with C-u M-X align and M-x align-regexp with= but neither works. Can someone come up with a solution?

like image 444
Lilianna Avatar asked Nov 21 '13 10:11

Lilianna


People also ask

How do I align in Emacs?

The \(\s-*\)\s- string is the regular expression that is used to align on, and the final \s- in that string tells emacs to align on a whitespace character.

How do I align text in Emacs?

First, we align the text. Select the text first, then press Ctrl + u then call align-regexp , with the regexp . * \([0-9,]+\). * then choose -1 for group, 1 for spacing, and n for repeat.


2 Answers

Why did you try to align on = ?? There isn't a single = in that example text.

Assuming your example is representative, you can align on the spaces. I'll use \s- (whitespace syntax) instead of an actual space, as the latter is harder to see.

C-uM-x align-regexp RET

  • Complex align using regexp: \(\s-*\)\s-
  • Parenthesis group to modify (justify if negative): 1
  • Amount of spacing (or column if negative): 3
  • Repeat throughout line? (y or n) y
like image 105
phils Avatar answered Oct 09 '22 23:10

phils


If you only want to align in the display and would prefer not to modify the file, you can try csv-mode (available in GNU ELPA). You'll have to specify SPC as the separator (by default it's comma or TAB), but you can then use the menu to tell the mode to align the columns.

like image 31
Stefan Avatar answered Oct 09 '22 21:10

Stefan