简体   繁体   English

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

[英]Emacs: align text, but not the entire line?

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. 我曾经使用的唯一一个支持这个的编辑器(我确定很多人都这样做)是Sublime Text 2,但希望emacs也有一个技巧。 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: 屏幕截图可能会澄清我的目标:

列化文本不对齐

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? 这可能吗?

Rather than using rectangles, you could use align-regexp . 您可以使用align-regexp ,而不是使用矩形。 Select the area then do: 然后选择区域:

Cu Mx 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 Mx my-align-after-commas 然后选择区域和Mx my-align-after-逗号

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 Mx columnize-text . 您选择要对齐的区域,然后选择Mx columnize-text

EmacsWiki page on this and others: 关于此和其他人的EmacsWiki页面:
http://www.emacswiki.org/emacs/CategoryAlignment http://www.emacswiki.org/emacs/CategoryAlignment

Emacs does let you select rectangular areas, and it does let you edit whole columns. Emacs确实允许您选择矩形区域,它可以让您编辑整个列。 To delete a rectangular area, set the mark at one corner, point at the other, and then Mx kill-rectangle . 要删除矩形区域,请将标记设置在一个角上,指向另一个角,然后设置Mx kill-rectangle You can add a rectangle of whitespace by marking it the same way, and then Mx open-rectangle . 您可以通过以相同的方式标记空白矩形,然后使用Mx open-rectangle

This is just the way that I know how to do this; 这就是知道如何做到这一点的方式; there are doubtless other, possibly better ways. 毫无疑问,其他可能更好的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM