简体   繁体   中英

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

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 .

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 Mx kill-rectangle . You can add a rectangle of whitespace by marking it the same way, and then Mx open-rectangle .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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