Right-align text in Emacs

对着背影说爱祢 提交于 2019-12-18 15:54:35

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/10895930/right-align-text-in-emacs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!