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.
To specify a rectangle for a command to work on, set the mark at one corner and point at the opposite corner. The rectangle thus specified is called the region-rectangle. If point and the mark are in the same column, the region-rectangle is empty. If they are in the same line, the region-rectangle is one line high.
The region-rectangle is controlled 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.
A rectangular region can also be marked using the mouse: click and drag C-M-mouse-1 from one corner of the rectangle to the opposite.
Kill the text of the region-rectangle, saving its contents as the
last killed rectangle (
Save the text of the region-rectangle as the last killed rectangle
Delete the text of the region-rectangle (
Yank the last killed rectangle with its upper left corner at point
Insert blank space to fill the space of the region-rectangle
open-rectangle). This pushes the previous contents of the
region-rectangle to the right.
Insert line numbers along the left edge of the region-rectangle
rectangle-number-lines). This pushes the previous contents of
the region-rectangle to the right.
Clear the region-rectangle by replacing all of its contents with spaces
Delete whitespace in each of the lines on the specified rectangle, starting from the left edge column of the rectangle.
Replace rectangle contents with string on each line
Insert string on each line of the rectangle.
Toggle Rectangle Mark mode (
When this mode is active, the region-rectangle is highlighted and can
be shrunk/grown, and the standard kill and yank commands operate on it.
The rectangle operations fall into two classes: commands to erase or insert rectangles, and commands to make blank rectangles.
There are two ways to erase the text in a rectangle: C-x r d
delete-rectangle) to delete the text outright, or C-x r
kill-rectangle) to remove the text and save it as the
last killed rectangle. In both cases, erasing the
region-rectangle is like erasing the specified text on each line of
the rectangle; if there is any following text on the line, it moves
backwards to fill the gap.
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 only records 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. Yank-popping is not defined for rectangles.
C-x r M-w (
copy-rectangle-as-kill) is the equivalent of
M-w for rectangles: it records the rectangle as the last
killed rectangle, without deleting the text from the buffer.
To yank the last killed rectangle, type C-x r y
yank-rectangle). The rectangle’s first line is inserted at
point, the rectangle’s second line is inserted at the same horizontal
position one line vertically below, and so on. The number of lines
affected is determined by the height of the saved rectangle.
For example, you can convert two single-column lists into a double-column list by killing one of the single-column lists as a rectangle, and then yanking it beside the other list.
You can also copy rectangles into and out of registers with C-x r r r and C-x r i r. See Saving Rectangles in Registers.
There are two commands you can use for making blank rectangles:
C-x r c (
clear-rectangle) blanks out existing text in the
region-rectangle, and C-x r o (
open-rectangle) inserts a
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 N (
line numbers along the left edge of the region-rectangle. Normally,
the numbering begins from 1 (for the first line of the rectangle).
With a prefix argument, the command prompts for a number to begin
from, and for a format string with which to print the numbers
(see Formatting Strings in The Emacs Lisp Reference
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.
The command C-x SPC (
whether the region-rectangle or the standard region is highlighted
(first activating the region if necessary). When this mode is enabled,
commands that resize the region (C-f, C-n etc.) do
so in a rectangular fashion, and killing and yanking operate on the
rectangle. See Killing and Moving Text. The mode persists only as long as the
region is active.
The region-rectangle works only when the mark is active. In particular, when Transient Mark mode is off (see Disabling Transient Mark Mode), in addition to typing C-x SPC you will need to activate the mark.
Unlike the standard region, the region-rectangle can have its corners extended past the end of buffer, or inside stretches of white space that point normally cannot enter, like in the middle of a TAB character.
When the region is active (see The Mark and the Region) and in rectangle-mark-mode,
C-x C-x runs the command
rectangle-exchange-point-and-mark, which cycles between the
four corners of the region-rectangle. This comes in handy if you want
to modify the dimensions of the region-rectangle before invoking an
operation on the marked text.