Rectangle commands operate on rectangular areas of the text: all the characters between a certain pair of columns, in a certain range of lines. Emacs has commands to kill rectangles, yank killed rectangles, clear them out, fill them with blanks or text, or delete them. Rectangle commands are useful with text in multicolumn formats, and for changing text into or out of such formats.
When you must specify a rectangle for a command to work on, you do it by putting the mark at one corner and point at the opposite corner. The rectangle thus specified is called the region-rectangle because you control it in much the same way as the region is controlled. But remember that a given combination of point and mark values can be interpreted either as a region or as a rectangle, depending on the command that uses them.
If point and the mark are in the same column, the rectangle they delimit is empty. If they are in the same line, the rectangle is one line high. This asymmetry between lines and columns comes about because point (and likewise the mark) is between two columns, but within a line.
open-rectangle). This pushes the previous contents of the region-rectangle rightward.
The rectangle operations fall into two classes: commands for deleting and inserting rectangles, and commands for blank rectangles.
There are two ways to get rid of the text in a rectangle: you can
discard the text (delete it) or save it as the “last killed”
rectangle. The commands for these two ways are C-x r d
delete-rectangle) and C-x r k (
either case, the portion of each line that falls inside the rectangle's
boundaries is deleted, causing any following text on the line to
move left into the gap.
Note that “killing” a rectangle is not killing in the usual sense; the rectangle is not stored in the kill ring, but in a special place that can only record the most recent rectangle killed. This is because yanking a rectangle is so different from yanking linear text that different yank commands have to be used. It is hard to define yank-popping for rectangles, so we do not try.
To yank the last killed rectangle, type C-x r y
yank-rectangle). Yanking a rectangle is the opposite of killing
one. Point specifies where to put the rectangle's upper left corner.
The rectangle's first line is inserted there, the rectangle's second
line is inserted at the same horizontal position, but one line
vertically down, and so on. The number of lines affected is determined
by the height of the saved rectangle.
You can convert single-column lists into double-column lists using rectangle killing and yanking; kill the second half of the list as a rectangle and then yank it beside the first line of the list. See Two-Column, for another way to edit multi-column text.
You can also copy rectangles into and out of registers with C-x r r r and C-x r i r. See Rectangle Registers.
There are two commands you can use for making blank rectangles:
C-x r c (
clear-rectangle) which blanks out existing text,
and C-x r o (
open-rectangle) which inserts a blank
rectangle. Clearing a rectangle is equivalent to deleting it and then
inserting a blank rectangle of the same size.
The command M-x delete-whitespace-rectangle deletes horizontal whitespace starting from a particular column. This applies to each of the lines in the rectangle, and the column is specified by the left edge of the rectangle. The right edge of the rectangle does not make any difference to this command.
The command C-x r t (
string-rectangle) replaces the
contents of a region-rectangle with a string on each line. The
string's width need not be the same as the width of the rectangle. If
the string's width is less, the text after the rectangle shifts left;
if the string is wider than the rectangle, the text after the
rectangle shifts right.
The command M-x string-insert-rectangle is similar to
string-rectangle, but inserts the string on each line,
shifting the original text to the right.