在Emacs中右对齐文本

Mic*_*ter 8 emacs text-alignment

有时,我在Emacs中有这样的文本文件:

some text     123     17
other text    1       0
still more    12      8
last one      1234    123
Run Code Online (Sandbox Code Playgroud)

我想对齐数字(使用空格),将其更改为以下内容:

some text      123     17
other text       1      0
still more      12      8
last one      1234    123
Run Code Online (Sandbox Code Playgroud)

如何在Emacs中完成?

phi*_*ils 13

align-regexp可以做到这一点.标记该区域,然后使用:

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

这应该是最简单的方法.

(编辑:事实上,你甚至不需要将最后的数字分开; \(\s-+[0-9]+\)对正则表达式也一样.)

见交互式提示和C-hf align-regexp RETalign-rules-list什么,实际上是做变量.

值得注意的是,通过为组指定负数,align-regexp设置justify属性:

`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.
Run Code Online (Sandbox Code Playgroud)

或者,各种表编辑选项也可以处理这个(例如org,ses,table-capture/release),或者你可以用elisp替换模式来处理它.

例如,如果文件已经使用空格进行对齐(untabify如果没有用于删除选项卡),并且所有行都是相同的长度(即尾随空格,则以下内容应该或多或少地执行您要查找的内容)如果最后一列的长度不同,则需要某些行.)

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