在 Emacs 中右对齐文本 [英] Right-align text in Emacs

查看:21
本文介绍了在 Emacs 中右对齐文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

如何在 Emacs 中完成此操作?

How can this be done in Emacs?

推荐答案

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

align-regexp can do this. Mark the region, and then use:

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

这应该是最简单的方法.

That should be the simplest approach.

(事实上,你甚至不需要把最后一个数字分开;(s-+[0-9]+)也适用于正则表达式.)

( In fact, you don't even need to separate out that final digit; (s-+[0-9]+) works just as well for the regexp.)

查看交互式提示和 Chf align-regexp RETalign-rules-list 变量用于实际执行的操作.

See the interactive prompts and C-hf align-regexp RET and the align-rules-list variable for what that is actually doing.

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

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.

或者,各种表格编辑选项也可以处理这个问题(例如 org、ses、table-capture/release),或者您可以使用 elisp 替换模式来做到这一点.

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.

例如如果文件已经在使用空格进行对齐(如果没有,您可以使用 untabify 删除制表符),并且所有行都是相同的长度(即,如果最后一列的长度不同,则某些行需要尾随空格).

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).

CM-% ([0-9]+)([[:space:]]+) RET,(format (concat "%" (number-to-string (1- (length &))) "d ") (string-to-number 1)) 返回

这篇关于在 Emacs 中右对齐文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆