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.
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.
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).
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.
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]
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.
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.
Finally, type C-x r o to add the new space to the table.
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.
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.
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.
7.5.1 CUA Rectangle EditingIf 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).
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.
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.
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. |