1

There are many nice column editing features in CUA mode. After hitting Ctrl+RETURN, binded to cua-set-rectangle-mark, you get some help in the minibuffer:

Minibuffer shows several M- shortcuts

But I can't find the shortcut to cut/kill rectangle text, e.g.: I want to get rid of all hardly stuff:

enter image description here

Any hints?

  • I found out today you can move through the corners of the rectangle using RET. So after C-RET, select rectangle, then press RET a few times and the point will move to each of the corners. Can be pretty useful :) – Vivi Sep 24 '15 at 10:28
3

CUA's help on rectangles (and other things) is slightly hidden away.

  • M-x find-library RET cua-base RET
  • Search the commentary for the section on "CUA rectangle support"

(Which is not to suggest that library commentaries in general are "hidden" -- they're a crucial aspect of Emacs' documentation which all users should know how to access. It's just that in this particular case cua-base is not necessarily the library you'd think to look in, given that the code in question lives in cua-rect.)

Maybe in the circumstances it's useful to quote it here. This is from Emacs 24.5.1:

CUA rectangle support
---------------------
Emacs's normal rectangle support is based on interpreting the region
between the mark and point as a "virtual rectangle", and using a
completely separate set of "rectangle commands" [C-x r ...] on the
region to copy, kill, fill a.s.o. the virtual rectangle.

cua-mode's superior rectangle support uses a true visual
representation of the selected rectangle, i.e. it highlights the
actual part of the buffer that is currently selected as part of the
rectangle.  Unlike emacs' traditional rectangle commands, the
selected rectangle always as straight left and right edges, even
when those are in the middle of a TAB character or beyond the end
of the current line.  And it does this without actually modifying
the buffer contents (it uses display overlays to visualize the
virtual dimensions of the rectangle).

This means that cua-mode's rectangles are not limited to the actual
contents of the buffer, so if the cursor is currently at the end of a
short line, you can still extend the rectangle to include more columns
of longer lines in the same rectangle.  And you can also have the
left edge of a rectangle start in the middle of a TAB character.
Sounds strange? Try it!

To start a rectangle, use [C-return] and extend it using the normal
movement keys (up, down, left, right, home, end, C-home,
C-end). Once the rectangle has the desired size, you can cut or
copy it using C-x and C-c (or C-w and M-w), and you can
subsequently insert it - as a rectangle - using C-v (or C-y).  So
the only new command you need to know to work with cua-mode
rectangles is C-return!

Normally, when you paste a rectangle using C-v (C-y), each line of
the rectangle is inserted into the existing lines in the buffer.
If overwrite-mode is active when you paste a rectangle, it is
inserted as normal (multi-line) text.

If you prefer the traditional rectangle marking (i.e. don't want
straight edges), [M-p] toggles this for the current rectangle,
or you can customize cua-virtual-rectangle-edges.

And there's more: If you want to extend or reduce the size of the
rectangle in one of the other corners of the rectangle, just use
[return] to move the cursor to the "next" corner.  Or you can use
the [M-up], [M-down], [M-left], and [M-right] keys to move the
entire rectangle overlay (but not the contents) in the given
direction.

[C-return] cancels the rectangle
[C-space] activates the region bounded by the rectangle

If you type a normal (self-inserting) character when the rectangle is
active, the character is inserted on the "current side" of every line
of the rectangle.  The "current side" is the side on which the cursor
is currently located. If the rectangle is only 1 column wide,
insertion will be performed to the left when the cursor is at the
bottom of the rectangle.  So, for example, to comment out an entire
paragraph like this one, just place the cursor on the first character
of the first line, and enter the following:
    C-return M-} ; ; <space>  C-return

cua-mode's rectangle support also includes all the normal rectangle
functions with easy access:

[M-a] aligns all words at the left edge of the rectangle
[M-b] fills the rectangle with blanks (tabs and spaces)
[M-c] closes the rectangle by removing all blanks at the left edge
      of the rectangle
[M-f] fills the rectangle with a single character (prompt)
[M-i] increases the first number found on each line of the rectangle
      by the amount given by the numeric prefix argument (default 1)
      It recognizes 0x... as hexadecimal numbers
[M-k] kills the rectangle as normal multi-line text (for paste)
[M-l] downcases the rectangle
[M-m] copies the rectangle as normal multi-line text (for paste)
[M-n] fills each line of the rectangle with increasing numbers using
      a supplied format string (prompt)
[M-o] opens the rectangle by moving the highlighted text to the
      right of the rectangle and filling the rectangle with blanks.
[M-p] toggles virtual straight rectangle edges
[M-P] inserts tabs and spaces (padding) to make real straight edges
[M-q] performs text filling on the rectangle
[M-r] replaces REGEXP (prompt) by STRING (prompt) in rectangle
[M-R] reverse the lines in the rectangle
[M-s] fills each line of the rectangle with the same STRING (prompt)
[M-t] performs text fill of the rectangle with TEXT (prompt)
[M-u] upcases the rectangle
[M-|] runs shell command on rectangle
[M-'] restricts rectangle to lines with CHAR (prompt) at left column
[M-/] restricts rectangle to lines matching REGEXP (prompt)
[C-?] Shows a brief list of the above commands.

[M-C-up] and [M-C-down] scrolls the lines INSIDE the rectangle up
and down; lines scrolled outside the top or bottom of the rectangle
are lost, but can be recovered using [C-z].
1

This one is pretty straightforward, M-k does it. It is not listed in the mini-buffer help, and asking for help with C-? does not show more help. It can be found on this link on rectangle editing with cua mode.

Interestingly, I can not find those comments on current cua-rect.el code.
But it is there: (cua--rect-M/H-key ?k 'cua-cut-rectangle-as-text).
As long as it took me some time to find this key binding, I thought it could be useful to others.

  • C-w works for me. Not sure if because I have some other option that does this. – Vivi Sep 24 '15 at 10:33
  • Can you let me know if C-w works for you as well? – Vivi Sep 24 '15 at 10:35
  • Yes, it works. I needed to test it with emacs -q, because I have C-w changed in my personal configuration. – nephewtom Sep 24 '15 at 12:20
  • @nephewtom It might be better to provide reference from the emacs source in its git master branch (cua-base.el) than point to a blog. The actual key mapping is done in cua-rect.el. That said, the non-CUA rectangle-mark-mode is pretty awesome in emacs 24.4+ ( gnu.org/software/emacs/manual/html_node/emacs/Rectangles.html ). – Kaushal Modi Oct 8 '15 at 21:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.