Emacs: Edit Column Text, Rectangle Commands

By Xah Lee. Date: . Last updated: .

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

try to delete the middle column
3c21 646f 6374
6874 6d6c 3e3c
2063 6861 7273
202f 3e0a 3c6d
6965 7770 6f72
2277 6964 7468

kill-rectangle

CommandKeyPurpose
kill-rectangleCtrl+x r kDelete selected column of text.
  1. Put your cursor before “646f”.
  2. Alt+x set-mark-commandCtrl+Space】.
  3. Move cursor to end of “6964”.
  4. Alt+x 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. Alt+x set-mark-commandCtrl+Space】.
  3. Move cursor to end of “6964”.
  4. Alt+x 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. Alt+x set-mark-commandCtrl+Space】.
  3. Move cursor to beginning of “dragon”.
  4. Alt+x rectangle-number-lines.

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

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)]

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

thanks to Markus Schütz

Rectangle Edit Topic

  1. Emacs: Edit Column Text, Rectangle Commands
  2. Emacs: Insert Alphabets A to Z
  3. Emacs: Copy Rectangle Region to kill-ring
  4. Emacs: Align Text
  5. Emacs: picture-mode, ASCII Diagram
  6. Emacs: Make Whitespaces Visible
Like my tutorial? Put $5 at patreon

Or Buy Xah Emacs Tutorial

Or buy a nice keyboard: Best Keyboard for Emacs

Ask me question on patreon