If you’re familiar with regions in Emacs, then you should consider the rectangle commands – a way of marking the region across lines, but operate on it in a column-intelligent manner. A character in a line is a “column”. Rectangles are edits on portions of the region between two buffer columns. The EmacsManual reads,

The rectangle commands operate on rectangular areas of the text: all the characters between a certain pair of columns, in a certain range of lines. […] 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.

For example, running ‘C-SPC’ (‘set-mark-command’) in column 3 of the buffer, and then moving down 4 lines and to column 8, will cover a rectangle that is 23 characters – 4 * (8 - 3 + 1) - 1. The location of the mark and the point can be interchanged, the mark does not have to be to the left of the point.

If you were to move down 4 lines but stay in the same column of 3, you would be in a rectangle of 0 characters. The “empty rectangle” is important for inserting a rectangle of text across the lines.

A good way to approach rectangles is to consider them a text-version of the “crop selection” tool one encounters in a graphics program or drawing tool. Use of rectangles is somewhat akin to operating on a narrowed portion of the buffer where the narrowing is from column number A → B instead of between point and mark.

Rectangle commands were originally part of Emacs’s PictureMode and therefore were intended as handlers on visual rectangles of data. Rectangles are useful in situations where editing is otherwise unnecessarily difficult, frustrating, or region-oriented Emacs commands are lacking.

One typically utilizes rectangles in a manner similar to that of the region. Rectangles can be killed or copied. They are not stored in the kill ring, however, but in a separate global variable called killed-rectangle. This variable is not organized as a ring or list, but holds only one rectangle. Accordingly, they cannot be yanked with the normal ‘yank’ command, i.e. C-y, but with ‘yank-rectangle’ (see below). This is convenient for yanking rectangles into ordinary buffers. As buffer objects, rectangles are fundamentally different from the region and aren’t afforded the same status. Moreover, this difference is orthogonal to most of the Emacs commands that operate on the region and rectangles can not be relied upon to always “Do the right thing”. This is in contrast to insertion of region registers which honor buffer markers and don’t clobber buffer text unless it was intended. See Registers below.

Because rectangles are a second-class citizen, using them programmatically in Elisp functions should be approached with caution especially with newer versions of Emacs (23 and later) which depart from a static line and column orientation buffer display with fixed width fonts and where 80 columns of text isn’t always 80 columns. Despite these detractors rectangles remain exceedingly useful for certain types of edits.

The EmacsManual explains how to use this feature.

Rectangle commands were originally part of Emacs’s PictureMode.

See also RectangleMark and ColumnNumberMode.

Traditional rectangle commands

Toggle Rectangle Mark mode

Activated by ‘C-x SPC’. This was introduced in Emacs version 24.4 and deserves a section of its own, as it seems to have introduced new ways of dealing with rectangular regions. The manual states that: “When this mode is active, the region-rectangle is highlighted and can be shrunk/grown, …”, which can indeed be done very conveniently with the ordinary movement commands. (Try it out.) It gets more versatile when you exchange-point-and-mark (C-x C-x), because then you can enlarge the region in any direction you wish. The difference to the “traditional” rectangle commands listed above is that the region now appears rectangular provided ‘transient-mark-mode’ is activated, and that the keybindings are transiently different (for, it seems, an arbitrary number of movement commands, and one “action” command). But the differences are apparently not limited to the user interface:

The manual then goes on to say that “… the standard kill and yank commands operate on it [i.e. the rectangular region].” What exactly these “standard kill and yank commands” are, however, is a bit mysterious: The only non-movement keys that seem to be transiently rebound are C-t (‘string-rectangle’) and C-o (‘open-rectangle’). You will, nevertheless, find that C-w kills the rectangle, M-w copies it, DEL deletes it, as one would expect (and, as I would say, is what the manual promises).

And then it gets interesting, because these kill and copy operations do use the kill ring, not ‘killed-rectangle’. There, the rectangle is primarily stored as a simple string (while ‘killed-rectangle’ stores it as a list of strings, one string for each line), but with the property yank-handler, which specifies the simple and intelligent (but undocumented) function ‘rectangle--insert-for-yank’ (which in turn inserts a list of strings all at the same column in consecutive lines). This means that you can simply insert a rectangle killed in this way with ‘yank’, i.e. C-y. (You can also hack around with ‘rectangle--insert-for-yank’ and insert strings of different length, by the way!)

This seems to mean that with ‘rectangle-mark-mode’, a more general way of handling rectangles seems to have been found, which is distinct from the other rectangle commands. The documentation (the docstring as well as Emacs and Emacs Lisp info manuals), however, still seems to be somewhat mum on that as of Emacs 26.3.

Use case: Copying tables from a Web browser

When copying and pasting tabular data from a Web page rendered in a Web browser, you often will get more than just what you want copied to the clipboard. Bring the text into Emacs – unless you were in Emacs already. If you want only one or some of the columns of the data, use the rectangle commands of Emacs. Hit ‘C-SPC’ at the beginning of the first entry in the column you want, and then move to the final entry in the last tabular column you want, then hit ‘C-x r k’. This kills the rows of the column(s) you’re interested in.

To move the columns you want to an external application like LibreOffice, Gnumeric or back to the Web browser, you’ll need to kill the text in Emacs so it is in the clipboard – it won’t make it there with the rectangle kill command. Switch to the *scratch* buffer or open some temporary buffer and yank the rectangular text with ‘C-x r y’, then kill the result regularly with ‘C-k’. It’s then ready to paste into the application. A little bit of trouble, but it’s well worth it.

Use case: Results from a database

A similar scenario where rectangular commands are useful is working with a database like MySQL or PostgreSQL.

After running some quick database queries that someone else needs, you run a query that selects a lot of columns for your verification but you’re only interested in only one or just a few of the columns. Rather than rewriting the query and re-executing it, or using Unix tools to programmatically extract the values, use the interactive rectangle commands of Emacs to extract the column(s) needed. Why bang on your database when your data is right there in the Emacs buffer ready for manipulation?

Use case: Output from shell commands

Many commands in the shell produce output in a column delimited format, for example the information and statistics about processes running in the operating system or the long, verbose format of file listings. Another fixed-column format are list of files located in a common path. For example,


You can delete the redundant text, “/usr/share/emacs/22.2/etc/”, with ‘C-x r d’, making:


With registers

As with regions, rectangles too can be stored to Registers though the interface to this feature is fussy in relation to other types of registers which manipulate/store buffer text in a separate manner than rectangles which are represented by a special list of strings.

When a register contains a rectangle, and that register’s rectangle is inserted with ‘insert-register’ the rectangle is inserted with its upper left corner at point. Use of rectangles in conjunction with registers is a powerful combination for managing columnar oriented data (think spreadsheets and tables). The caveat is that when rectangle registers are inserted their text is inserted in the current line and underneath it on successive lines overwriting the existing buffer content.

I’d say that the “overwriting” behavior of rectangle registers is consistent with ‘yank-rectangle’, so I see no problem with this. Is there something I’m missing? – AaronHawley

It is consistent with ‘yank-rectangle’. However, it is inconsistent with ‘yank’. Contrast: ‘yank’ → ‘insert-register’ (with-region) ‘yank-rectangle’ → ‘insert-register’. – mon_key

Unfortunately, the design of registers in Emacs is to “do the right thing” with what ever “object” is in the register – point, window, frame, number, text or rectangle. I can understand your point. You’re probably suggesting there be a method for inserting a rectangle on lines of their own. It would be desirable for registers to behave this way with rectangles to handle your wish. I know I have to run ‘open-line’ many times to make room, instead. – AaronHawley

Replace/yank rectangle

Sometimes one wishes to replace a rectangle with another one. Currently this involves copying the source rectangle, deleting the destination rectangle, popping the mark and yanking. I need this often enough to create a dedicated command for this:

 (defun yank-replace-rectangle (start end)
   "Similar like yank-rectangle, but deletes selected rectangle first."
   (interactive "r")
   (delete-rectangle start end)
 (global-set-key (kbd "C-x r C-y") 'yank-replace-rectangle)

Yeah, you have to set point to the beginning of the rectangle when you delete, so that you are positioned to yank. That avoids the ‘pop-to-mark-command’. Your command only works when you mark the rectangle to delete with point at the end. – AaronHawley