Emacs: Edit Column Text, Rectangle Commands
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
| Command | Key | Purpose |
|---|---|---|
kill-rectangle | 【Ctrl+x r k】 | Delete selected column of text. |
- Put your cursor before “646f”.
- Alt+x
set-mark-command【Ctrl+Space】. - Move cursor to end of “6964”.
- Alt+x
kill-rectangle.
replace-rectangle
| Command | Key | Purpose |
|---|---|---|
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
| Command | Key | Purpose |
|---|---|---|
yank-rectangle | 【Ctrl+x r y】 | Paste 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
| command | Key | Purpose |
|---|---|---|
rectangle-number-lines | 【Ctrl+x r N】 | Insert 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
- 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.
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
- Move cursor to the before “dragon”.
- Alt+x
set-mark-command【Ctrl+Space】. - Move cursor to before “medusa”.
- Alt+x
universal-argument【Ctrl+u】. - Now, Alt+x
rectangle-number-lines. It will prompt you to enter arguments. - Type
65(Letter A has Unicode codepoint 65. a is 97). [see ASCII Table] - Remove the default
%2d, type%c.(the “%c” is for character format)
[see Emacs: Insert Alphabets A to Z]
More Rectangle Commands
Here's other rectangle commands, they are less often used.
| command | Key | Purpose |
|---|---|---|
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】 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.
Copy Rectangle to kill-ring/clipboard
When you Alt+x 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: Copy Rectangle Region to kill-ring
thanks to Markus Schütz
Emacs: picture-mode, ASCII diagram
Emacs: picture-mode, ASCII diagram