Êíèãà: Learning GNU Emacs, 3rd Edition

7.5 Rectangle Editing

When you mark regions to move or delete, they always cover the full width of the window. Editing by region is fine for most of the work that you do in Emacs. But what if you wanted to edit a table? Regions cover the full width of the window, so they can't handle columns. Emacs offers another way to define areas to delete, copy, and move around: using rectangles. Rectangles are just what they sound like: rectangular areas that you define and manipulate using special rectangle editing commands. Editing with rectangles is useful whenever you want to move or delete vertical columns of information; for instance, moving a column of a table or rearranging fields in a dataset.

For example, let's say you want to edit the following table, moving the "Hours" column to the right side. There's no way to do this using regions, but it's easy to do if you learn some rectangle editing commands.

Initial state:


A flextime schedule.

You define a rectangle the same way you define a region; the commands you use after marking the area tell Emacs whether you want to work with a region or a rectangle. (This is a good time to let go of your mouse and use keyboard commands for marking the text. Highlighting remains horizontal when you're working with rectangles and will only confuse you as you begin to think rectangularly. Of course, there's nothing wrong with using the mouse to move the cursor quickly; just don't use it to highlight text.)

Before we start working with these columns, select the buffer with C-x h and untabify it by typing M-x untabify. Rectangle editing works best with files that do not contain tab characters.

To define a rectangle, move the cursor to the upper-left corner and set the mark by pressing C-Space, then move the cursor to the lower-right corner of the rectangle. Once you're at the lower-right corner of the rectangle, move one character farther. Why move one character farther? Remember that when you define a region, the character that the cursor is on isn't part of the region. (The character that the mark is on is part of the region.)

Let's define a rectangle that covers the second column of our table.

Move to the H in Hours and type C-Space


The mark is set at the upper-left corner of the rectangle to be moved.

Move the cursor to the space following the bottom-right corner of the rectangle, the c in chipmunk.


The cursor follows the bottom-right corner of the rectangle.

Now that the rectangle is marked, we want to delete it and then move it. The command to delete a rectangle so you can retrieve it elsewhere is C-x r k (for kill-rectangle).

Type: C-x r k


The rectangle is deleted; it's in a special rectangle kill buffer.

Once again, when you mark a rectangle, you put the cursor on the upper-left corner, set the mark, then move to the lower-right corner of the rectangle and over one more space. Emacs expects rectangles to be rectangles. If necessary, it pads an area with spaces to make up the straight line on the right side.

You can move anywhere on the screen and reinsert the rectangle last killed with the yank-rectangle command, C-x r y. To put the "Hours" column on the right side of the table, we move the cursor following the cell phone column.

Place the cursor following Cell and press M-10 Space to move to a good location to paste the "Hours" column:


Move the cursor to where we want to reinsert the rectangle.

Type: C-x r y


Emacs inserts the rectangle we killed earlier.

Emacs inserts the rectangle exactly where you tell it to. We moved past the cell phone column and then added some space between the cell phone and hours columns. Otherwise, Emacs would have blithely inserted the hours column into the middle of the cell phone column. Note that there's no equivalent of the kill ring for rectangles. You can yank only the most recent rectangle.[38]

Killing and yanking rectangles requires practice. Once you get the hang of the procedure, it is an easy way to edit tables and other column-dependent material.

A few other commands create blank rectangles. For example, let's say we want to put four more spaces between the cell phone and hours columns. To do this, we set the mark, move to the bottom of the column, move forward four spaces, then type C-x r o (for open-rectangle). This command inserts a blank rectangle and pushes the remaining text to the right.

Move the cursor to the H in Hours and type C-Space


Emacs sets the mark at the upper-left corner of the rectangle.

Now we need to define the amount of space we want to insert. Move down to the bottom of the rectangle (the "Alvin" line) and then move to the hyphen between 6:00 and 3:00.

Move the cursor following 6:00.


The lower right corner of the rectangle is defined.

Finally, type C-x r o to add the new space to the table.

Type C-x r o


Emacs inserts a blank rectangle that is four spaces wide. It moves the rest of the table to the right.

The clear-rectangle command wipes out text, leaving a blank rectangle in its place. It's just as though you had erased a column on a blackboard. Like the blackboard column, the text column that is wiped out is gone, not stored in the rectangle kill buffer. To continue with our example, let's say that after reviewing the schedule, all those involved agreed that they'd rather not have their cell phones listed.

Move the cursor to the C in Cell and type C-space.


The upper-left corner of the rectangle to be cleared is marked.

Move to the space following the last phone number and type: C-x r c


The clear-rectangle command removes the "Cell Phone" column and leaves a blank space in its place.

As you can see, the spacing of our table still isn't perfect; you'd probably want to use the delete-rectangle command[39] to delete the extra space between the second and the third columns. To delete the blank space without storing it, start by moving the cursor to the space following the longest email address and press C-Space to set the mark, then move to the opposite corner of the box you want to delete and type C-x r d.

On the header line, move to the column after the longest email address and press C-Space


The upper-left corner of the rectangle to be deleted is marked.

Move a few spaces before 6:00 on the last line and type C-x r d


The delete-rectangle command deletes the blank space.

If you're doing some really fancy table editing, being able to store several rectangles is helpful. That way, you can have every column as a rectangle, as well as having a rectangle for the exact amount of blank space to put between each column. You can store rectangles in registers by typing C-x r r r where r is any alphanumeric character, including punctuation. To insert a rectangle you've stored, type C-x r i r. Registers don't persist between sessions.

Table 7-5 lists rectangle commands.

Table 7-5. Rectangle commands

Keystrokes Command name Action
C-x r k kill-rectangle Delete a rectangle and store it.
C-x r d delete-rectangle Delete a rectangle and do not store it.
C-x r y yank-rectangle Insert the last rectangle killed.
C-x r c clear-rectangle Using spaces, blank out the area marked as a rectangle and do not store it.
C-x r o open-rectangle Insert a blank rectangle in the area marked.
C-x r r r copy-rectangle-to-register Copy rectangle to register r (where r is any character) .
C-x r i r insert-register Insert rectangle from register r (where r is any character).
(none) delete-whitespace-rectangle If a rectangle includes initial whitespace, deletes it, narrowing rectangle.
C-x r t string Enter string-rectangle Change contents of marked rectangle to string (if string is narrower or wider than rectangle, dimensions change accordingly).
(none) string-insert-rectangle Prompts for string and inserts rectangle.

Îãëàâëåíèå êíèãè

Îãëàâëåíèå ñòàòüè/êíèãè

Ãåíåðàöèÿ: 1.854. Çàïðîñîâ Ê ÁÄ/Cache: 3 / 0
ïîäåëèòüñÿ
Ââåðõ Âíèç