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

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

问题描述

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

 一些文本123 17 
其他文字1 0
更多12 8
最后一个1234 123

我想右侧对齐数字(使用空格),将其更改为:

 某些文本123 17 
其他文字1 0
更多12 8
最后一个1234 123

如何在Emacs中完成?

解决方案

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



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



这应该是最简单的方法。



编辑:其实你甚至不需要分开最后的数字; \(\s - + [0-9] + \ )同样适用于正则表达式。)



查看交互式提示和 Ch f align-regexp RET align-rules-list



值得注意的部分是通过为该组指定一个负数, align-regexp 设置对齐属性:

 `justify'
它可以用regexp和group来标识一个包含m的
字符组矿石不仅仅是空格
的字符。默认情况下,
中的任何非空格字符也将被删除,同时对齐
对齐字符。但是,如果`justify'属性
设置为非零值,那么该组中的初始空格
个字符将被删除。这有
对右对齐的字符的影响,
,可以用于outdenting或只是纯旧的right-
对齐。

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



eg如果文件已经使用空格进行对齐,则以下内容应该做的更多或更少(您可以使用 untabify 删除选项卡,如果不是)并且所有行都是相同的长度(如果最后一列的长度不同,则在某些行中需要尾随空格)。



CM - % \([0-9] + \)\([[:space:]] + \) RET code> \,(format(concat%(number-to-string(1-(length \&)))d)(string-to-number \1) code> RET


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

How can this be done in Emacs?

解决方案

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

C-uM-x 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.)

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

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.

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.

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

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

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

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