Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

aligning or prettifying code in emacs

I remember this was possible in emacs, but don't know how. If I have something like:

'abc' => 1,  
'abcabc' =>2,  
'abcabcabc' => 3,  

How can I align the keys, arrows and values to something like this?

'abc'       => 1,  
'abcabc'    => 2,  
'abcabcabc' => 3,  

Cheers

like image 916
719016 Avatar asked Jun 02 '11 16:06

719016


2 Answers

You can also use the align command instead of align-regexp. The difference is that align automatically chooses the regular expression(s) to use based on the major-mode of the buffer. So if you are trying to align a block of variable initializations and assignments in a c-mode file, then it will automatically do the right thing without you needing to think of the regular expressions which are needed. Can be convenient.

For example select the following lines:

int x = 3;
double y = 9.0;
unsigned int z = 6;
const char c = 'A';

And type M-x align RET. The result is:

int          x = 3;
double       y = 9.0;
unsigned int z = 6;
const char   c = 'A';

I should add, though, that this will not always work. If there are no regular expressions defined for the major-mode of the current buffer, then the call to align will do nothing. Then, you need to fall back on align-regexp. But this is hardly a large inconvenience. I actually use align-regexp fairly frequently. For convenience, I have defined an alias to save myself a few key-strokes:

(defalias 'ar #'align-regexp)
like image 27
A. Levy Avatar answered Sep 17 '22 20:09

A. Levy


  • Select the region.

  • Type M-x align-regexp RET

  • Type = and hit enter.

like image 138
ShreevatsaR Avatar answered Sep 16 '22 20:09

ShreevatsaR