Align elements of a matrix to the right

I have a matrix in C++

``````Matrix<double> A = {
{31, -13, 0, 0, 0, -10, 0, 0, 0},
{-13, 35, -9, 0, -11, 0, 0, 0, 0},
{0, -9, 31, -10, 0, 0, 0, 0, 0},
{0, 0, -10, 79, -30, 0, 0, 0, -9},
{0, 0, 0, -30, 57, -7, 0, -5, 0},
{0, 0, 0, 0, -7, 47, -30, 0, 0},
{0, 0, 0, 0, 0, -30, 41, 0, 0},
{0, 0, 0, 0, -5, 0, 0, 27, -2},
{0, 0, 0, -9, 0, 0, 0, -2, 29}
};
``````

Tried `M-- M-x align` but that gives

``````Matrix<double> A = {
{31,  -13, 0,   0,   0,   -10, 0,   0,  0},
{-13, 35,  -9,  0,   -11, 0,   0,   0,  0},
{0,   -9,  31,  -10, 0,   0,   0,   0,  0},
{0,   0,   -10, 79,  -30, 0,   0,   0,  -9},
{0,   0,   0,   -30, 57,  -7,  0,   -5, 0},
{0,   0,   0,   0,   -7,  47,  -30, 0,  0},
{0,   0,   0,   0,   0,   -30, 41,  0,  0},
{0,   0,   0,   0,   -5,  0,   0,   27, -2},
{0,   0,   0,   -9,  0,   0,   0,   -2, 29}
};
``````

which is not particularly visually-appealing! How can I align elements to the right? (`,`s on one column)

• What's the value of `align-rules-alist` (`C-h v RET align-rules-alist RET`)? Are you running `M-x align` with a prefix argument (e.g. using `C-u M-x align-rules alist` in vanilla emacs, or `4 SPC SPC align-rules-alist` in spacemacs)? – Croad Langshan Dec 2 '18 at 16:16
• I think what's happening is that emacs is trying to line up the numbers, not the end of the lines, and regards the `-` in numbers as part of the thing that should be aligned. The details of alignment behaviour are I think configured by `align-rules-alist`. I guess it's working the way you see in part because of the value of that variable (in particular, `c-comma-delimiter`). – Croad Langshan Dec 2 '18 at 16:19
• @CroadLangshan Vanilla emacs with the default rules list. I still have no idea how alignment in Emacs works. But thanks to your tip I now know that emacs is trying to capture whitespaces... If `align` doesn't work, how can I solve it by `align-regexp`? – Mike Manilone Dec 3 '18 at 9:25