Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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>();
like image 251
Cheeso Avatar asked May 27 '09 14:05

Cheeso


3 Answers

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.

like image 121
ShreevatsaR Avatar answered Nov 17 '22 13:11

ShreevatsaR


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)
like image 41
phils Avatar answered Nov 17 '22 13:11

phils


M-x align should do the trick.

like image 7
Nathaniel Flath Avatar answered Nov 17 '22 12:11

Nathaniel Flath