In Emacs, how to line up equals signs in a series of initialization statements?
Asked Answered
N

3

30

I saw this somewhere, but cannot find it now. Is there a built-in function in emacs, or does someone have elisp, to line up all the equals signs in a series of inititialization statments in cc-mode?

Before:

int t=9;
Graphics g = new Graphics();
List<String> list = new List<String>();

After:

int          t    = 9;
Graphics     g    = new Graphics();
List<String> list = new List<String>();
Neva answered 27/5, 2009 at 14:22 Comment(0)
C
46

Use M-x align-regexp (here, M-x align-regexp RET = RET). You can also add an "alignment rule" to the variable align-rules-list, so that in future M-x align will do it. See the documentation (C-h f align) for details.

Commonality answered 27/5, 2009 at 14:35 Comment(4)
Ok, I've bound this to C-x |. How did I use emacs for so long and not know about this? Thanks for the help.Neva
The only problem with this is that it "tabifies" the output, which is probably not what you want. Do you know any way to prevent this?Ramakrishna
harpo: I've added a new answer for your question.Overwhelm
This URL helped with making this into a fn: #3633620Slaty
O
18

This is in response to harpo's comment to ShreevatsaR's answer:

The only problem with this is that it "tabifies" the output, which is probably not what you want. Do you know any way to prevent this?

Here's what I did to resolve that issue:

;; Align with spaces only
(defadvice align-regexp (around align-regexp-with-spaces)
  "Never use tabs for alignment."
  (let ((indent-tabs-mode nil))
    ad-do-it))
(ad-activate 'align-regexp)
Overwhelm answered 15/11, 2011 at 0:14 Comment(2)
This is not a great answer to the original question, but valuable, valuable, valuable. (I think I just Ballmered.)Thundershower
It took me some time to understand what this does...Zoril
E
7

M-x align should do the trick.

Eupatrid answered 27/5, 2009 at 14:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.