Some notes on rectangle regions in GNU Emacs

Today I asked a question over on the Fediverse :

In GNU Emacs, is there any way to 'undo' the loss of a selection region, especially a rectangle selection ? I am forever going through the dance of C-x SPC, select a rectangle region, do something that fumbles the action I wanted to do, rectangle region disappears, I grind my teeth, and I would like an easy way to recover should there be one.

(I can't spot anything in the manual or in MELPA/ELPA packages, but I may not have looked hard enough.)

Thanks to a very helpful reply and some additional experimentation, I found my answer. What I want to do under normal circumstances is C-x C-x, C-x C-x again, and then C-x SPC.

What I called a rectangle selection is more correctly called a region rectangle , a rectangular region. In GNU Emacs, a region in general is the text between point (the cursor) and an active mark . When I do the wrong thing with a region rectangle active, the mark and the region (rectangle) deactivate, but the mark remains. When I do the first C-x C-x ( exchange-point-and-mark ), point and mark flip and the mark is reactivated so I have an active region again, although not a region rectangle. However my cursor is now at the start of the region, not where it was when I fumbled things. Doing C-x C-x flips point and mark again, leaving the mark and region active and returning my cursor to where it was. Then C-x SPC ( rectangle-mark-mode ) switches from a regular region to a region rectangle.

(Until now I hadn't realized that C-x SPC was a toggle, not a stand-alone key binding that started a special rectangular region mode. Learning this was a useful revelation . If you never use it with a region already active, C-x SPC looks like a stand-alone thing because it immediately activates a mark at point if there isn't already an active mark and thus a region.)

With a region rectangle active, C-x C-x cycles between the four corners of the region (as documented, cf ). If the region is zero characters wide (for example, because you're drawing it down the left side of a block of text so you can use 'C-x r t' to insert '> ' in front of every line), this is equivalent to just swapping between the start and the end, which is what C-x C-x does with a regular region active.

(You can also use string-insert-rectangle for this insertion of a prefix, especially since it defaults to '> ' as the string to insert. But 'C-x r t' on a zero-width rectangle fits my mental model better, since it's close to how I do it in Vim block editing, and also it actually shows me the effect of the text I'm inserting. And there's also comment-region , which in a text buffer will ask you what 'comment' string you want to use and which works with a regular, non-finicky region.)

In fact, even 'making' a region rectangle with C-x SPC isn't necessary for what I normally do (inserting text at the start of all lines), because now that I actually read the documentation (and do some experiments), the 'C-x r ...' collection of commands don't necessarily require an active region rectangle. C-x SPC will let you visualize the rectangle they'll act on, which is potentially important, but if I set a mark at the start of a paragraph (in the leftmost column) and then move to the end of the paragraph (or just after it) but still in the leftmost column, 'C-x r t' will do what I want. M-{ and M-} are obvious movement commands to use here.

(There are some commands that behave differently depending on a region rectangle and a regular region, like C-w. I think I'm going to keep using C-x SPC to make region rectangles before C-x r t, simply so I can keep things straight in my head.)

One of the lessons I draw from this is that I want to try to read Emacs documentation more carefully, rather than skim it. I obviously found C-x SPC in the Rectangles documentation, but I didn't read carefully enough to spot that it was a toggle (or think deeply enough about the convenient implications of that). Another is that I probably have a lot of Emacs semi-knowledge that has decayed to the level of superstition by now, such as my understanding of regions and the mark (until I refreshed it by reading The Mark and the Region documentation for this entry).

(Emacs is truly a voyage of discovery, in some ways much more so than Vim.)

PS: This means that if I accidentally deactivated a regular region, I can get it back with C-x C-x then C-x C-x again. Or just C-x C-x if I don't care about which end is point and which is mark, but usually I do (and I want mark to be the start and point the end). And in something else I want to remember, there's C-M mouse-1 to start sweeping out a region rectangle; after it covers something, I can switch to regular cursor movements for more precision.