Emacs: Edit Column Text, Rectangle Commands

By Xah Lee. Date: . Last updated: .

This page shows you how to edit text in a rectangular region.

kill-rectangle

try to delete the middle column
3c21 646f 6374
6874 6d6c 3e3c
2063 6861 7273
202f 3e0a 3c6d
6965 7770 6f72
2277 6964 7468
CommandKeyPurpose
kill-rectangleCtrl+x r kDelete selected column of text.
  1. Put your cursor before “646f”.
  2. Call set-mark-commandCtrl+Space】.
  3. Move cursor to end of “6964”.
  4. Call kill-rectangle.

replace-rectangle

CommandKeyPurpose
replace-rectangleCtrl+x r tReplace text in a selected column.
try change this

 3c21 646f 6374
 6874 6d6c 3e3c
 2063 6861 7273
 202f 3e0a 3c6d
 6965 7770 6f72
 2277 6964 7468

to this

 3c21 cat 6374
 6874 cat 3e3c
 2063 cat 7273
 202f cat 3c6d
 6965 cat 6f72
 2277 cat 7468
  1. Put your cursor before “646f”.
  2. Call set-mark-commandCtrl+Space】.
  3. Move cursor to end of “6964”.
  4. Call replace-rectangleCtrl+x r t】.

Paste Rectangle

CommandKeyPurpose
yank-rectangleCtrl+x r yPaste a column of text (after you used kill-rectangle).

The yank-rectangle is useful when you want to move a column of text.

try to swap the 2nd and 3rd columns
3c21 646f 6374
6874 6d6c 3e3c
2063 6861 7273
202f 3e0a 3c6d
6965 7770 6f72
2277 6964 7468

Note: you need to call kill-rectangle first.

Insert a Column of Numbers

commandKeyPurpose
rectangle-number-linesCtrl+x r NInsert numbers in a vertical column. (new in emacs 24) 〔➤see Emacs 24.1 New Features (released 2012-06)

It is very useful for writing a ordered list of items.

try change this

cat
dog
bird
dragon

to this

1 cat
2 dog
3 bird
4 dragon
  1. Put your cursor before the “cat”.
  2. Call set-mark-commandCtrl+Space】.
  3. Move cursor to beginning of “dragon”.
  4. Call rectangle-number-lines.

To start with a different number, call universal-argumentCtrl+u】 right before rectangle-number-lines.

Insert A to Z

You can also insert letters A to Z by using rectangle-number-lines.

try change this

1 cat
2 creatures
  dragon
  phoenix
  elf
  hydra
  medusa
3 dog
4 bird

to this

1 cat
2 creatures
  A. dragon
  B. phoenix
  C. elf
  D. hydra
  E. medusa
3 dog
4 bird
  1. Move cursor to the before “dragon”.
  2. Call set-mark-commandCtrl+Space】.
  3. Move cursor to before “medusa”.
  4. Call universal-argumentCtrl+u】.
  5. Now, call rectangle-number-lines. It will prompt you to enter arguments.
  6. Type 65 (Letter A has Unicode codepoint 65. a is 97). 〔➤see ASCII Table
  7. Remove the default %2d , type %c. (the “%c” is for character format)

〔➤see Emacs Lisp: Insert Alphabets A to Z

More Rectangle Commands

Here's other rectangle commands, they are less often used.

commandKeyPurpose
delete-rectangleCtrl+x r dsimilar to kill-rectangle, but doesn't save to a special clipboard.
string-insert-rectanglesimilar to replace-rectangle, but doesn't replace the rectangle text.
clear-rectangleCtrl+x r creplace with space
delete-whitespace-rectangleDelete all whitespace following starting corner. (this is alias to close-rectangle)
delimit-columns-rectangle
open-rectangleCtrl+x r oInsert space to the rectangle. (shifting text to the right)
copy-rectangle-as-killCtrl+x r Alt+wsimilar to kill-rectangle but doesn't delete.

rectangle-mark-mode

rectangle-mark-modeCtrl+x Space】 will visually highlight the rectangle. This is new in emacs 24.4 〔➤see Emacs 24.4 New Features (released 2014-10)

Call rectangle-mark-mode, then move cursor to highlight a rectangular area. Press ⌦ Delete will delete text in that rectangle area.

Copy Rectangle to kill-ring/clipboard

When you call kill-rectangle, the text does not go into emacs kill-ring nor the OS's clipboard.

If you want to copy a rectangle region, see:

Emacs Lisp: Copy Rectangle Region to kill-ring

thanks to Markus Schütz

Emacs: picture-mode, ASCII diagram

Emacs: picture-mode, ASCII diagram

Like it? Buy Xah Emacs Tutorial. Thanks.