开发者

Emacs: align text, but not the entire line?

开发者 https://www.devze.com 2023-04-05 22:50 出处:网络
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 declarati

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:

Emacs: align text, but not the entire line?

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 fal开发者_StackOverflow中文版l into alignment.

Is this possible?


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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号