Emacs: Edit Column Text, Rectangle
Delete a Column of Text
kill-rectangle
【Ctrl+x r k】-
Delete a column of text in Text Selection
💡 TIP: this is useful for deleting first n characters of every line.
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-rectangle
【Ctrl+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
string-rectangle
【Ctrl+x r t】 (aliasreplace-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 string-rectangle 【Ctrl+x r t】
Insert a Column of Numbers
rectangle-number-lines
【Ctrl+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-rectangle
【Ctrl+x r d】-
Similar to
kill-rectangle
, but doesn't copy it. string-insert-rectangle
-
Similar to
string-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. (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.
2012-07-23 thanks to Markus Schütz