Emacs中的右对齐文本
有时,我在Emacs中有这样一个文本文件:Emacs中的右对齐文本,emacs,text-alignment,Emacs,Text Alignment,有时,我在Emacs中有这样一个文本文件: some text 123 17 other text 1 0 still more 12 8 last one 1234 123 我想将数字对齐(使用空格),将其更改为如下内容: some text 123 17 other text 1 0 still more 12 8 last one 1234 123
some text 123 17
other text 1 0
still more 12 8
last one 1234 123
我想将数字对齐(使用空格),将其更改为如下内容:
some text 123 17
other text 1 0
still more 12 8
last one 1234 123
如何在Emacs中做到这一点?
align regexp
可以做到这一点。标记区域,然后使用:
C-uM-xalign regexp
RET\(\s-+[0-9]*\)[0-9]
RET-1
RET4
RETy
这应该是最简单的方法
(Edit:事实上,您甚至不需要分离出最后一位;\(\s-+[0-9]+\)
对regexp也同样有效。)
请参阅交互式提示和C-hfalign regexp
RET以及align 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.
或者,各种表格编辑选项也可以处理此问题(例如,org、ses、表格捕获/发布),或者您也可以使用elisp替换模式进行处理
e、 g.如果文件已经使用空格对齐(如果没有,您可以使用untabify
删除制表符),并且所有行的长度都相同(即,如果最后一列的长度不同,则某些行上需要尾随空格),那么下面的内容应该或多或少满足您的要求
C-M-%
\([0-9]+\)\([[:space:][]+\)
RET\,(格式(concat“%”(数字到字符串(1-(长度\&)))“d”)(字符串到数字\1))
RET表格模式,组织模式在这里不是必需的,虽然通常非常有用,但最终我更喜欢基于rect
的模式。回顾这一点,我很开心,在align regexp
模式的对齐组中看到非空白,并在阅读我自己的解释之前思考“哦,不,我在这里写了什么!?”(显然,对齐不是我经常使用的功能!)