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.[4]

[4] You can, however, store rectangles in registers, providing the effective equivalent of the kill ring. More on this shortly.

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[5] 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.

[5] Like all Emacs delete commands, delete-rectangle doesn't store what you delete in the kill ring.

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.


7.5.1 CUA Rectangle Editing

If you are familiar with CUA mode, which is part of Emacs starting with 21.3.5, you may know that it provides cut and paste key sequences familiar to Windows users, as in C-x to cut and C-v to paste (see Chapter 13). The second most commonly touted feature of CUA mode is its superior rectangle support.

We've just looked at a myriad of rectangle commands. CUA's rectangle support is far simpler. By learning essentially one command, you can cut and paste rectangles in CUA mode.

Unfortunately at present, CUA mode support is standard but not nuanced on Emacs 21.3.5. You either take the whole enchilada or you don't. To turn it on, select C-x/C-c/C-v cut and paste (CUA) from the Options menu. If you don't generally like to use the CUA keybindings for cut and paste, you might turn this option on only when you are doing rectangle editing.

To select a rectangle, type Shift-Enter. Emacs starts to highlight in a dark pink color by default. You extend the highlighting with normal cursor movement keys (the mouse does not work at present).

Move to the C in Cell and type: Shift-Enter

The upper-left corner of our rectangle is marked (Windows).


The minibuffer displays an array of CUA mode rectangle commands. For now, we'll just mark the rectangle and experiment with one of these commands momentarily.

Move the cursor to the last number in Alvin's phone number.

The rectangle is marked (Windows).


Note that the marked rectangle isn't strictly rectangular in shape. The phone numbers form a true rectangle, but in order to create a rectangle that includes the column header, we need to ask CUA mode to "pad" the rectangle using M-p, one of the commands listed in the minibuffer earlier.

Type: M-p

The pad command makes this a true rectangle (Windows).


We can now cut or paste the rectangle using C-x or C-v respectively. This is just a taste of the CUA mode rectangle commands. You can explore more of them on your own. We thought you should be aware of this method as an alternative to the more keyboard-intensive rectangle commands that have been part of Emacs for many years.



Learning GNU Emacs
Learning GNU Emacs, Third Edition
ISBN: 0596006489
EAN: 2147483647
Year: 2003
Pages: 161

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net