Emacs: Edit Column Text, Rectangle Commands
This page shows you how to edit text in a rectangular region.
Add a prefix to every line (such as # or //)
- Move to the beginning of first line you want.
- Press Ctrl+Space (
set-mark-command
) to mark cursor position. - Move cursor to the beginning of the last line.
- Then Alt+x
string-rectangle
【Ctrl+x r t】 - 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-rectangle
【Ctrl+x r k】.
Delete a column of text
kill-rectangle
【Ctrl+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
- Put your cursor before 646f.
- Alt+x
set-mark-command
【Ctrl+Space】. - Move cursor to end of 6964.
- Alt+x
kill-rectangle
.
replace-rectangle
replace-rectangle
【Ctrl+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
- Put your cursor before “646f”.
- Alt+x
set-mark-command
【Ctrl+Space】. - Move cursor to end of “6964”.
- Alt+x
replace-rectangle
【Ctrl+x r t】.
Paste Rectangle
yank-rectangle
【Ctrl+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-lines
【Ctrl+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
- Put your cursor before the “cat”.
- Alt+x
set-mark-command
【Ctrl+Space】. - Move cursor to beginning of “dragon”.
- Alt+x
rectangle-number-lines
.
To start with a different number,
Alt+x
universal-argument
【Ctrl+u】 right before rectangle-number-lines
.
More Rectangle Commands
Here's other rectangle commands, they are less often used.
delete-rectangle
【Ctrl+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-rectangle
【Ctrl+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-rectangle
【Ctrl+x r o】- Insert space to the rectangle. (shifting text to the right)
copy-rectangle-as-kill
【Ctrl+x r Alt+w】-
Similar to
kill-rectangle
but doesn't delete.
rectangle-mark-mode
rectangle-mark-mode
【Ctrl+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