Right-align text in Emacs

emacs, text-alignment

Solution

`align-regexp` can do this. Mark the region, and then use:

C-uM-x `align-regexp` RET `\(\s-+[0-9]*\)[0-9]` RET `-1` RET `4` RET `y`

That should be the simplest approach.

(Edit: In fact, you don't even need to separate out that final digit; `\(\s-+[0-9]+\)` works just as well for the regexp.)

See the interactive prompts and C-hf `align-regexp` RET and the `align-rules-list` variable for what that is actually doing.

The noteworthy part is that by specifying a negative number for the group, `align-regexp` sets the `justify` attribute:

`justify'   
It is possible with `regexp' and `group' to identify a
character group that contains more than just whitespace
characters.  By default, any non-whitespace characters in
that group will also be deleted while aligning the
alignment character.  However, if the `justify' attribute
is set to a non-nil value, only the initial whitespace
characters within that group will be deleted.  This has
the effect of right-justifying the characters that remain,
and can be used for outdenting or just plain old right-
justification.

Alternatively the various table-editing options can also deal with this (e.g. org, ses, table-capture/release), or you could do it with an elisp replacement pattern.

e.g. The following should do more or less what you're looking for, provided that the file is already using spaces for alignment (you can use `untabify` to remove the tabs if not), and that all lines are the same length (i.e. trailing spaces are needed on some lines if the final column is of varying length).

C-M-% `\([0-9]+\)\([[:space:]]+\)` RET `\,(format (concat "%" (number-to-string (1- (length \&))) "d ") (string-to-number \1))` RET

Problem

Sometimes, I have a text file like this in Emacs: ``` some text 123 17 other text 1 0 still more 12 8 last one 1234 123 ``` I would like to right-align the numbers (using spaces), changing it into something like this: ``` some text 123 17 other text 1 0 still more 12 8 last one 1234 123 ``` How can this be done in Emacs?

Original source

Related problems