2010-01-16

rectangles and cua

CUA-mode is a minor-mode that enables the use of Ctrl-X/C/V for cut/copy/paste, as is customary in many computer programs. Of course, it's a bit different in emacs, as it predates CUA and all of those programs. With esp. Ctrl-X (C-x) being in heavy use as a prefix-key already, it's unlikely to change.

CUA-mode has a clever trick to solve that problem – C-x for cut only works when a selection is active, and when no other key is pressed shortly. Otherwise, C-x behaves as usual. This works quite nicely, but personally, I don't use it, as I already have the Emacs key bindings in my muscle memory. Still, it can be useful for people migrating from other CUA-based editors. See e.g. CuaMode for more information.

However, apart from the C-x-trick, CUA also has some nice functionality for rectangular selections. These are sometimes quite useful, and during emacs-fu's prehistory there was already an article about it. The method there works, but with CUA, it is much easier.

So, let's turn it on; put the following in your .emacs:

(setq cua-enable-cua-keys nil) ;; only for rectangles
(cua-mode t)

Now, just put your cursor anywhere, and press C-RET (Ctrl + Enter). You have now started a rectangular selection! There rest is pretty straightforward, you can cut, copy and paste with the normal Emacs key bindings..

I could go into more detail, but the best way is to see it in action in this wonderful screen cast by Mark Mansour. I especially like the way you can add numbered lists.

13 comments:

Laurent Le Brun said...

The cua-mode has been in my .emacs file for a long time, not for the CUA bindings, but for the Rectangles feature. I especially love it for incrementing numbers (or inserting numbers).

lifealgorithms said...

Ah, this is really practical when working with pseudo-table formated files. conkeror should also have something like that.

Oscar said...

Oh, this was really handy. Now I only have to learn to use it... :-)

Anonymous said...

I guess there are little typo:
article about it about it

djcb said...

@anonymous: fixed, thx.

Anonymous said...

How do you replace all occurrences of a string in a rectangle in cua-mode

djcb said...

@Anonymous: when have your rectangle selected, you can use cua-replace-in-rectangle (bound to 'M-r') to replace inside the rectangle.

Anonymous said...

cua-replace-in-rectangle only replaces 1st occurrence, how do you replace all occurrences within a rectangle?

djcb said...

@Anonymous: it should replace all, and it works fine for me at least. Maybe double-check your regexp?

Anonymous said...

It doesn't work for a simple expression like a single letter where the letter occurs more than once on a line. It does replace the 1st occurrence for all lines in the rectangular block, but not the rest of the occurrences. M-r doesn't allow ! like the normal replace. What if you only wanted to replace a single occurrence and wanted to be prompted for the rest?

djcb said...

@Anonymous: yes, that looks like a limitation; maybe file a bug against emacs? Or maybe hack cua-replace-in-rectangle to make it work like you want.

Anonymous said...

How can you overwrite a rectangular selection using C-v? If insert or overwrite is toggled, it only inserts the rectangular selection you previously copied? I used to be able to use edt-paste-rectangle with the older version of emacs?

Anonymous said...

Now emacs supports C-x r N to insert a column of numbers. You can use the prefix C-u to specify the format and the number to use as start ..