aligning or prettifying code in emacs
Asked Answered
J

2

20

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

Justin answered 2/6, 2011 at 16:10 Comment(0)
L
37
  • Select the region.

  • Type M-x align-regexp RET

  • Type = and hit enter.

Lysozyme answered 2/6, 2011 at 16:39 Comment(8)
Here M-x means either Alt+x or Esc followed by x. RET means Enter.Lysozyme
I used align-regexp somewhat often, but not often enough to warrant its own dedicated key combination, so I put (defalias 'ar 'align-regexp) in my .emacs file.Defend
That is nice, but I met here the problem that the lowest number of spaces in which it is aligns is two tabs. Even with emacs -Q it aligns with one tab. Is there a way to align with one space as the lowest alignment?Typewriting
@Hi-Angel: Could you give an example of what you mean? In my opinion one should anyway have set indent-tabs-mode to nil so that Emacs never inserts tab characters, and with that setting it aligns with only spaces for me (and 0 spaces if possible, e.g. on the longest line). I haven't tried without that setting.Lysozyme
@Lysozyme as an example you can try to align the answer below of A.Levy, that will produce the same result. Btw, are you too suppose that the tabs are evil? ☺ Well, tbh I didn't saw yet an arguments that could convince me to change an alignment to a spaces only. Probably I will when I encounter a problems with this, but yet I really like that one tab takes 4 times lower space than 4 spaces.Typewriting
Also every time you're writing a code in an established environment, where an indentation, say, 8 spaces, basically you don't even need to change a tab space. Though for an alignment it is still important anyway, yeah…Typewriting
@Hi-Angel: Yes I tried the example in A. Levy's answer, and it works fine for me. (E.g. the first line becomes int_x__________=_3; (used _ for space), with no tabs.) As for the rest: I don't know about "evil", but having tabs in a file is a damned inconvenience and leads to all sorts of problems (like this one). I really don't care about 3 bytes, and it's much more valuable to have a file that looks the same on every system, and contains no weird stuff I can't see. Anyway, if you don't want to set indent-tabs-mode to nil globally, you may be able to defadvice it around align-regexp.Lysozyme
As I found a time, here's the solution, and yes, via «defadvice».Typewriting
E
22

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)
Erbe answered 3/6, 2011 at 19:26 Comment(4)
Cool, is there auto-align-mode or something? To align every line authomatically according to some magic rules or something?Provincial
@Provincial I don't know of an auto-align mode, but there is an "align-current" command that will try to intelligently pick a region around the current cursor position and align that. I have align-current mapped to a key combination so that I can use it as soon as I finishing typing something I want aligned.Erbe
Also, see this for an example of how to add align rules (in this case, making CoffeeScript symbol value pairs aligned).Dermot
@A.Levy if in your example was a comments after the variables, the comments would have been aligned too, that's not cool (I just found this align command, and stucked with such a problem. A possible solution M-x align-regexp RET = RET)Typewriting

© 2022 - 2024 — McMap. All rights reserved.