Right-align text in Emacs -
sometimes, have text file in emacs:
some text 123 17 other text 1 0 still more 12 8 last 1 1234 123 i right-align numbers (using spaces), changing this:
some text 123 17 other text 1 0 still more 12 8 last 1 1234 123 how can done in emacs?
align-regexp can this. mark region, , use:
c-um-x align-regexp ret \(\s-+[0-9]*\)[0-9] ret -1 ret 4 ret y
that should simplest approach.
(edit: in fact, don't need separate out final digit; \(\s-+[0-9]+\) works regexp.)
see interactive prompts , c-hf align-regexp ret , align-rules-list variable doing.
the noteworthy part specifying negative number group, align-regexp sets justify attribute:
`justify' possible `regexp' , `group' identify character group contains more whitespace characters. default, non-whitespace characters in group deleted while aligning alignment character. however, if `justify' attribute set non-nil value, initial whitespace characters within group deleted. has effect of right-justifying characters remain, , can used outdenting or plain old right- justification. alternatively various table-editing options can deal (e.g. org, ses, table-capture/release), or elisp replacement pattern.
e.g. following should more or less you're looking for, provided file using spaces alignment (you can use untabify remove tabs if not), , lines same length (i.e. trailing spaces needed on lines if final column of varying length).
c-m-% \([0-9]+\)\([[:space:]]+\) ret \,(format (concat "%" (number-to-string (1- (length \&))) "d ") (string-to-number \1)) ret
Comments
Post a Comment