[英]Right-align text in Emacs
Sometimes, I have a text file like this in Emacs: 有时,我在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? 如何在Emacs中完成?
align-regexp
can do this. align-regexp
可以做到这一点。 Mark the region, and then use: 标记该区域,然后使用:
Cu Mx align-regexp
RET \\(\\s-+[0-9]*\\)[0-9]
RET -1
RET 4
RET y
Cu Mx
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.) ( 编辑:事实上,你甚至不需要将最后一位数字分开;
\\(\\s-+[0-9]+\\)
对于正则表达式也同样适用。)
See the interactive prompts and Ch f align-regexp
RET and the align-rules-list
variable for what that is actually doing. 请参阅交互式提示和Ch f
align-regexp
RET以及align-rules-list
变量以了解实际执行的操作。
The noteworthy part is that by specifying a negative number for the group, align-regexp
sets the justify
attribute: 值得注意的是,通过为组指定负数,
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.
Alternatively the various table-editing options can also deal with this (eg org, ses, table-capture/release), or you could do it with an elisp replacement pattern. 或者,各种表编辑选项也可以处理这个(例如org,ses,table-capture / release),或者你可以用elisp替换模式来处理它。
eg 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 (ie trailing spaces are needed on some lines if the final column is of varying length). 例如,如果文件已经使用空格进行对齐(如果没有,则可以使用
untabify
删除选项卡)以及所有行的长度相同(即尾随空格),以下内容应该或多或少地执行您要查找的内容如果最后一列的长度不同,则在某些行上需要。
CM-% \\([0-9]+\\)\\([[:space:]]+\\)
RET \\,(format (concat "%" (number-to-string (1- (length \\&))) "d ") (string-to-number \\1))
RET CM-%
\\([0-9]+\\)\\([[:space:]]+\\)
RET \\,(format (concat "%" (number-to-string (1- (length \\&))) "d ") (string-to-number \\1))
RET
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.