Emacs:对齐文本,但不是整行?

发布于 2024-12-05 15:37:23 字数 402 浏览 1 评论 0原文

我使用过的唯一支持此功能的编辑器(不过我相信很多编辑器都支持)是 Sublime Text 2,但希望 emacs 也有一个妙招。我工作的一些类具有相当声明性的语法,如果源代码保持列化,那么它们看起来会更干净。当新行将列的长度增加一两个空格时,问题就出现了……然后您必须重新对齐每一行。

屏幕截图可能澄清了我的意思:

Columnized text out ofalign

这里我添加了一行,其中一个如果它的列比其他列延伸得更远,那么我想再次重新对齐它。通常,我只是在箭头和空格键周围重复循环手指,直到我逐行重新对齐所有内容,但如果有一种方法可以只选择一个矩形区域并用力,那就太好了该区域的文本非空白部分要对齐。

这可能吗?

The only editor I've ever used that supports this (I'm sure lots do, however) is Sublime Text 2, but hopefully emacs has a trick up its sleeve too. Some classes I work on that have a fairly declarative syntax look cleaner if the source is kept columnized. The problem comes in when a new row increases the length of a column by a space or two... you then have to work through each row re-aligning it.

A screenshot probably clarifies what I'm getting at:

Columnized text out of alignment

Here I've added a row in which one if its columns extends further than the other columns, so I'd like to realign it all again. Ordinarily I'd just repeat-cycle my fingers around the arrows and the space bar until I've re-aligned everything row-by-row, but it'd be good if there was a way to just select a rectangular area and force text non-whitespace parts of that area to fall into alignment.

Is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一杆小烟枪 2024-12-12 15:37:23

您可以使用 align-regexp,而不是使用矩形。选择区域,然后执行:

Cu Mxalign-regexp RET ,\(\s-*\) RET 1 RET 1 RET y

如果您经常这样做,请创建一个函数:

(defun my-align-after-commas (beg end)
  (interactive "r")
  (align-regexp beg end ",\\(\\s-*\\)" 1 1 t))

然后选择区域并MX my-align-after-commas

Rather than using rectangles, you could use align-regexp. Select the area then do:

C-u M-x align-regexp RET ,\(\s-*\) RET 1 RET 1 RET y

If you do this a lot, make a function:

(defun my-align-after-commas (beg end)
  (interactive "r")
  (align-regexp beg end ",\\(\\s-*\\)" 1 1 t))

Then select the area and M-x my-align-after-commas

慈悲佛祖 2024-12-12 15:37:23

还有 columnize 库 可以将其转换

(defvar enform-keywords-statements
  '( "AT END"  "AT  START" "CLOSE" "DECLARE" "DELINK" "DICTIONARY"     
     "EXIT" "FIND" "FOOTING" "LINK" "LIST" "OPEN" "PARAM" "SET"            
     "SUBFOOTING" "SUBTITLE" "TITLE" )
  "List of ENFORM statement keywords. Used to create the `font-lock-keywords' table.")

为:

(defvar enform-keywords-statements
  '( "AT END"      "AT  START"   "CLOSE"       "DECLARE"     "DELINK"      
     "DICTIONARY"  "EXIT"        "FIND"        "FOOTING"     "LINK"        
     "LIST"        "OPEN"        "PARAM"       "SET"         "SUBFOOTING"  
     "SUBTITLE"    "TITLE"        )
  "List of ENFORM statement keywords. Used to create the `font-lock-keywords' table.")

您选择要对齐的区域然后是Mx columnize-text

有关此内容和其他内容的 EmacsWiki 页面:
http://www.emacswiki.org/emacs/CategoryAlignment

There's also the columnize library which can transform this:

(defvar enform-keywords-statements
  '( "AT END"  "AT  START" "CLOSE" "DECLARE" "DELINK" "DICTIONARY"     
     "EXIT" "FIND" "FOOTING" "LINK" "LIST" "OPEN" "PARAM" "SET"            
     "SUBFOOTING" "SUBTITLE" "TITLE" )
  "List of ENFORM statement keywords. Used to create the `font-lock-keywords' table.")

into this:

(defvar enform-keywords-statements
  '( "AT END"      "AT  START"   "CLOSE"       "DECLARE"     "DELINK"      
     "DICTIONARY"  "EXIT"        "FIND"        "FOOTING"     "LINK"        
     "LIST"        "OPEN"        "PARAM"       "SET"         "SUBFOOTING"  
     "SUBTITLE"    "TITLE"        )
  "List of ENFORM statement keywords. Used to create the `font-lock-keywords' table.")

You select the region you want to align and then M-x columnize-text.

EmacsWiki page on this and others:
http://www.emacswiki.org/emacs/CategoryAlignment

蓦然回首 2024-12-12 15:37:23

Emacs 确实允许您选择矩形区域,并且它确实允许您编辑整个列。要删除矩形区域,请在一个角上设置标记,指向另一个角,然后Mx Kill-rectangle。您可以通过以相同的方式标记它来添加空白矩形,然后使用 Mx open-rectangle

这就是知道如何做到这一点的方法;毫无疑问还有其他可能更好的方法。

Emacs does let you select rectangular areas, and it does let you edit whole columns. To delete a rectangular area, set the mark at one corner, point at the other, and then M-x kill-rectangle. You can add a rectangle of whitespace by marking it the same way, and then M-x open-rectangle.

This is just the way that I know how to do this; there are doubtless other, possibly better ways.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文