Did you ever wish you could select a box of text from a document for copying, killing, or yanking purposes? You can. In Emacs, a selection of text specified by any two opposite of its four corners is called a rectangle; the following sections show you how to use rectangles effectively.
All you have to do to specify a rectangle is set the mark in any one of its four imaginary corners and then move point to the opposite corner. When you do so, the imaginary box of text framed by the two corners you've selected is the current rectangle.
When you run one of the following rectangle commands, it works on the text
you've selected. All of the rectangle commands of Emacs begin with
C-x r, and they're explained in the following
There are a few ways to get rid of text you've marked as a rectangle. Although all the functions described here erase the current rectangle, each of them has a different effect.
C-x r k, to kill the current rectangle. It works like
kill-line function that you're familiar
with: It removes all the characters in the rectangle and doesn't replace them
with anything. Any characters to the right of the rectangle automatically move
to the left.
This function saves the contents of the rectangle in a special rectangle kill area, not the normal kill ring (see Use the kill ring).
Try it now:
- Move point to the beginning of the stanza that begins with What the
hammer, and type
C-spaceto set the mark.
- Move point to just before the word terrors to specify a rectangle of four lines (with, incidentally, three words on each line).
- Kill the rectangle you selected: Type
C-x r k.
The rectangle you selected is gone. Notice how all the text to the right of it moves over to fill the rectangle's place.
To delete the characters in a rectangle you've marked without saving
delete-rectangle, which is bound to
C-x r d. This command deletes the area of the
rectangle, so the effect looks just the same as if you killed the rectangle (as
just described); the difference is that you aren't able to
yank back the text you killed.
(However, as you learned back in the first tutorial, you can always undo
an action you just performed. If you type
after you delete or kill a rectangle, the original text of the rectangle is
Both of the two commands for erasing rectangles that have just been described
remove the entire space that the rectangle occupied. But you can also
clear it, replacing the entire area with space characters by running the
clear-rectangle function, which is bound to
C-x r c.
- Move point to just before the word dare in the line On what wings
dare he aspire?, and set the mark by typing
- To specify a small rectangle consisting of the word dare on two lines, move point to just after the word dare in the line below it.
C-x r cto clear the rectangle you just defined.
Notice that point moves to the lower-left corner of the rectangle.
Finally, you can open a rectangle -- use the rectangle to specify an
area where whitespace should be added. To do this, mark a rectangle, and then
r o. Doing so fills the entire rectangle with
space characters and pushes all text in the rectangle to the right.
Try it now:
- Move point to the top of the buffer and set the mark by typing
- Move point to right after the Co in Could in the bottom stanza.
- Open the rectangle by typing
C-x r o.
To yank the contents of the last-killed rectangle at point, run
yank-rectangle, which is bound to
C-x r y. This command inserts, at point, the last
rectangle that was killed. When the insertion occurs, all existing text on all
lines of the rectangle is pushed to the right.
- Move point to the beginning of the stanza whose text contains what the
M-< M-1 M-5 C-n M-2 C-f.
C-x r yto yank the rectangle.
Notice that the small rectangle you just cleared wasn't the one that was yanked; when you clear a rectangle, it's not saved -- only rectangles that are killed are saved, and you can only yank the last rectangle that was killed.
Also notice that point moved to just after the lower-right corner of the rectangle, and the minibuffer reports that the mark has been set. When you yank a rectangle, the mark is set to the upper-left corner of the rectangle you yanked.
To replace a rectangle with whitespace but save its original contents so that you can yank it later, first kill it, then yank it, and then clear it:
- Set the mark just before the first Did he in the second-to-last
stanza by typing
M-< M-2 M-2 C-n M-2 C-f C-space.
M-6 C-f C-nto move point to just after the he in the next line.
C-x r kto kill the rectangle.
C-pto move point to the upper-left corner of the rectangle you just killed.
- Yank it with
C-x r y.
- Clear it with
C-x r c.
At this point, you can yank the rectangle somewhere else. The original location of the rectangle is now full of whitespace.
- Move point up to just after the word wings by typing
M-1 M-6 C-p M-1 M-4 C-f.
- Yank the rectangle with
C-x r y.
Notice that point moves to the lower-right corner of the rectangle you just yanked. The mark has been set in the upper-left corner of the rectangle, as reported in the minibuffer.
Table 1 lists the various Emacs rectangle commands you just learned, giving their function names and describing their meanings.
Table 1. Emacs commands for using rectangles
||Marks one corner of a rectangle (point marks the opposite corner).|
||Kills the current rectangle and saves it in a special rectangle buffer.|
||Deletes the current rectangle and doesn't save it for yanking.|
||Clears the current rectangle, replacing the entire area with whitespace.|
||Opens the current rectangle, filling the entire area with whitespace and moving all text from the rectangle to the right.|
||Yanks the contents of the last-killed rectangle at point, moving all existing text to the right.|