19

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

719016
  • 9,922
  • 20
  • 85
  • 158

2 Answers2

36
  • Select the region.

  • Type M-x align-regexp RET

  • Type = and hit enter.

ShreevatsaR
  • 38,402
  • 17
  • 102
  • 126
  • 2
    Here `M-x` means either Alt+x or Esc followed by x. `RET` means Enter. – ShreevatsaR Jun 02 '11 at 16:41
  • 2
    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. – Sean Jun 04 '11 at 04:21
  • 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? – Hi-Angel Oct 22 '14 at 05:07
  • @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. – ShreevatsaR Oct 22 '14 at 05:11
  • @ShreevatsaR 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. – Hi-Angel Oct 22 '14 at 05:21
  • 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… – Hi-Angel Oct 22 '14 at 05:35
  • 1
    @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. – ShreevatsaR Oct 22 '14 at 05:36
  • 1
    As I found a time, [here's the solution](http://stackoverflow.com/a/25164056/2388257), and yes, via «defadvice». – Hi-Angel Oct 22 '14 at 19:15
21

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)
A. Levy
  • 29,056
  • 6
  • 39
  • 56
  • Cool, is there auto-align-mode or something? To align every line authomatically according to some magic rules or something? – s9gf4ult May 15 '14 at 10:30
  • @s9gf4ult 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. – A. Levy May 19 '14 at 17:36
  • 1
    Also, see [this](https://coderwall.com/p/mmr_hw) for an example of how to add `align` rules (in this case, making CoffeeScript symbol value pairs aligned). – rsenna Oct 14 '14 at 14:02
  • @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`)* – Hi-Angel Oct 22 '14 at 04:50