Emacs: Edit Column Text, Rectangle Commands

By Xah Lee. Date: . Last updated: .

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

Add a prefix to every line (such as # or //)

  1. Move to the beginning of first line you want.
  2. Press Ctrl+Space (set-mark-command) to mark cursor position.
  3. Move cursor to the beginning of the last line.
  4. Then Alt+x string-rectangleCtrl+x r t
  5. Then type what you want to insert, then Enter.

The command string-rectangle can be used to insert a vertical column of string across mulitple lines at any column position, not just at the beginning of lines.

Delete the first few n chars of every line

Mark Ctrl+Space the beginning of first line and move cursor to the last line, and move it to the right n chars. Then call kill-rectangleCtrl+x r k】.

Delete a column of text

kill-rectangleCtrl+x r k
Delete selected column of text.
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 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

replace-rectangleCtrl+x r t
Replace 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

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

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

Note: you need to call kill-rectangle first.

Insert a Column of Numbers

rectangle-number-linesCtrl+x r N
Insert numbers in a vertical column. (new in emacs 24) [see Emacs 24.1 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.

delete-rectangleCtrl+x r d
Similar to kill-rectangle, but doesn't save to a special clipboard.
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. This is new in emacs 24.4 [see Emacs 24.4 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

Emacs: Comment, Uncomment, Prefix Lines, Rectangle Edit


Emacs Tutorial

Quick Start

Font

Split Window

File

Buffer

Copy/Paste

Find Replace

Unicode

Whitespace

Rectangle Edit

Line Wrap

Shell

View Special File

Editing Brackets

Org Mode

HTML

Emacs Efficiency

Misc