【问题标题】:Right-align text in EmacsEmacs 中的文本右对齐
【发布时间】:2012-06-09 09:20:51
【问题描述】:

有时,我在 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

如何在 Emacs 中做到这一点?

【问题讨论】:

  • table-mode, org-mode 在这里不是必须的,虽然通常很有用
  • 最后我更喜欢基于rectsolution

标签: emacs text-alignment


【解决方案1】:

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

CuMx align-regexp RET \(\s-+[0-9]*\)[0-9] RET -1 RET kbd> 4 RET y

这应该是最简单的方法了。

编辑:事实上,您甚至不需要分离出最后一个数字;\(\s-+[0-9]+\) 也适用于正则表达式。)

查看交互式提示和 Chf 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.

另外,各种表格编辑选项也可以处理这个问题(例如 org、ses、表格捕获/发布),或者您可以使用 elisp 替换模式来处理。

例如以下应该或多或少地完成您正在寻找的内容,前提是该文件已经使用空格进行对齐(如果没有,您可以使用untabify 删除制表符),并且所有行的长度相同(即尾随如果最后一列的长度不同,则某些行需要空格)。

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

【讨论】:

  • 我只是很开心回顾这个,看到align-regexp 模式的对齐组中的非空格,并想“哦不,我在这里写了什么!?”在阅读我自己的解释之前,为什么这很好:)(显然,证明不是我经常使用的功能!)
猜你喜欢
  • 1970-01-01
  • 2012-04-06
  • 1970-01-01
  • 2012-11-16
  • 2011-09-02
  • 2014-07-23
相关资源
最近更新 更多