Emacs: Edit Column Text, Rectangle

By Xah Lee. Date: . Last updated: .

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

Delete a Column of Text

kill-rectangleCtrl+x r k

Delete a column of text in Text Selection

💡 TIP: this is useful for deleting first n characters of every line.

emacs kill-rectangle 2023-10-16
emacs kill-rectangle 2023-10-16
try to delete the middle column

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

1 Put cursor before 646f
2 M-x set-mark-command 【Ctrl+Space】
3 Move cursor to end of 6964
4 M-x kill-rectangle

Paste Rectangle

yank-rectangleCtrl+x r y

Paste a column of text (after you used kill-rectangle).

💡 TIP: This 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

1 Select the last column.
2 M-x kill-rectangle
3 Select the first empty column.
4 M-x yank-rectangle

Replace a Column of Text

replace-rectangleCtrl+x r t】 (alias of string-rectangle)

Replace a column of text in Text Selection

💡 TIP: this is useful for adding prefix to every line.

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 cursor before 646f
2 M-x set-mark-command 【Ctrl+Space】
3 Move cursor to end of 6964
4 M-x replace-rectangle 【Ctrl+x r t】

Insert a Column of Numbers

rectangle-number-linesCtrl+x r N

Insert sequence of numbers in a vertical column. (new in Emacs 24 (Released 2012-06))

To start with a different number, call Universal Argument (prefix arg) first.

💡 TIP: It is useful for writing a ordered list of items.

try change this

cat
dog
bird

to this

1 cat
2 dog
3 bird

1 Put cursor to beginning of “cat”
2 M-x set-mark-command 【Ctrl+Space】
3 Move cursor to beginning of “bird”
4 M-x rectangle-number-lines

Insert A to Z Using rectangle-number-lines

Here's how to insert A to Z using rectangle-number-lines.

try change this

cat
dog
bird

to this

A. cat
B. dog
C. bird

solution:

1. Move cursor to before cat.
2. set-mark-command 【Ctrl+Space】.
3. Move cursor to before bird.
4. universal-argument 【Ctrl+u】
5. M-x rectangle-number-lines. It will prompt you to enter arguments.
6. Type 65 (Letter A has Unicode codepoint 65.).
7. Remove the default %2d , type %c. (the “%c” is for character format)

Other Rectangle Commands

Here's other rectangle commands, they are less often used. (i never use them.)

delete-rectangleCtrl+x r d

Similar to kill-rectangle, but doesn't copy it.

string-insert-rectangle

Similar to replace-rectangle, but doesn't replace the rectangle text.

clear-rectangleCtrl+x r c

Replace with space.

delete-whitespace-rectangle

Delete all whitespace following starting corner. (this is alias to close-rectangle)

delimit-columns-rectangle

open-rectangleCtrl+x r o

Insert space to the rectangle. (shifting text to the right)

copy-rectangle-as-killCtrl+x r Alt+w

Similar to kill-rectangle but doesn't delete.

rectangle-mark-mode

rectangle-mark-modeCtrl+x Space
Visually highlight the rectangle. (new in Emacs 24.4 (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

Emacs: Comment, Uncomment, Rectangle Edit