0

I am trying to align code using align-regex but I could not figure out how to solve the below problem.

This is what I have:

public class Person
{
    public static String id = "";
    private String name = "";
    public int age = 10;

} 

This is what I am trying to get:

public class Person
{
    public  static String id =       "";
    private String name   =  "name";
    public  int    age    =  10;

} 

Actually I just want to separate them into each column.

I have tried M-x align-regex RET \(s-*\)\s- and many more but none is working. Please help me solve this or advice me.

1 Answer 1

0

If you don't mind re-indenting afterwards:

C-uM-x align-regex RET \(\s-+\) RETRETRETy

To re-activate the mark and re-indent should then be:

C-xC-xTAB


To avoid the need to re-indent, we need to enable it to ignore as much of the initial space as possible:

C-uM-x align-regex RET \s-*\(\s-+\) RETRETRETy

3
  • it's work really nice but it adds one more TAB to the code everytime I try the command. Is that I done something wrong?
    – tammary
    Oct 14, 2018 at 7:52
  • Does stackoverflow.com/a/8129994/324105 help?
    – phils
    Oct 14, 2018 at 8:28
  • And if you are indenting with only tabs and aligning with only spaces, then that gives you an easy way to ignore the indentation, by aligning on \( +\) (i.e. one-or-more spaces).
    – phils
    Oct 14, 2018 at 8:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.