This page shows how to {copy, cut, insert} a column of text in emacs, how to manipulate ASCII-diagrams, and how to align text.
Emacs has about 8 commands for manipulating a column of text. In my 12 years of using emacs daily, the most useful to me are these two. I use them few times everyday.
| Command | Key | Purpose |
|---|---|---|
kill-rectangle | 【Ctrl+x r k】 | Delete selected column of text. |
replace-rectangle | 【Ctrl+x r t】 | Replace text in a selected column. (This is alias to string-rectangle) |
Here's a example of how to use it. In the following, put your cursor before the “1”, then mark 【Ctrl+Space】, then move it to end of “2”, then call kill-rectangle.
.............. ..1xxx..oooo.. ..xxxx..oooo.. ..xxxx..oooo.. ..xxxx..oooo.. ..xxxx..oooo.. ..xxxx..oooo.. ..xxxx..oooo.. ..xxxx..oooo.. ..xxxx..oooo.. ..xxx2..oooo.. ..............
The other emacs command i often want but always forgot is:
| 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 “x” and “o” blocks in the above sample text.
| command | Key | Purpose |
|---|---|---|
rectangle-number-lines | 【Ctrl+x r N】 | Insert numbers in a vertical column. (new in emacs 24) |
It is very useful for writing a ordered list of items. Example:
1. x 2. x 3. x 4. x
You can first make a text selection and it'll insert at the column of your cursor start position.
To start with a different number, press 【Ctrl+u】 first.
Here's other rectangle commands, but i've never used them:
| command | Key | Purpose |
|---|---|---|
delete-rectangle | 【Ctrl+x r d】 | like kill-rectangle, but doesn't save to a special clipboard. |
string-insert-rectangle | ◇ | like 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) |
These “rectangle” commands are useful for ASCII diagrams. Here's a examples of ASCII diagram.
_____________________________________________ | | | Sequence | | ______ ________________________________ | | | | | | | | | List | | Array | | | | | | ________ ________ | | | |______| | | | | | | | | | | Vector | | String | | | | | |________| |________| | | | | ____________ _____________ | | | | | | | | | | | | | Char-table | | Bool-vector | | | | | |____________| |_____________| | | | |________________________________| | |_____________________________________________|
I almost never do ASCII diagram, yet, i still find kill-rectangle and replace-rectangle useful everyday when editing source code.
Emacs's kill-rectangle does not put the text to operating system's clipboard. Here a command to copy a column of text to OS's clipboard.
(defun copy-rectangle-to-clipboard (p1 p2) "Copy region as column (rectangle) to operating system's clipboard. This command will also put the text in register 0. See also: `kill-rectangle', `copy-to-register'." (interactive "r") (let ((x-select-enable-clipboard t)) (copy-rectangle-to-register ?0 p1 p2) (kill-new (with-temp-buffer (insert-register ?0) (buffer-string) )) ) )
You can give it a key. 〔☛ Emacs: How to Define Keys〕
cua-mode has a nice feature that makes emacs rect commands much easier to use, because it highlights just the rectangle, and user can just use normal copy/paste keys to manipulate the rect.
cua-mode to turn it on.(thanks to Markus Schütz)
Emacs has several commands that's helpful for alignment. One of the most useful is align-regexp. For example, suppose you have:
tom = 5 jenny = 8 mary = 7
and you want to align them by the equal sign. Just select the text, then call align-regexp then give “=”. Then you get:
tom = 5 jenny = 8 mary = 7
Emacs also has a picture-mode, designed for drawing ASCII diagrams. I've never used it.
Open a new file. Then, call picture-mode, then call describe-mode 【F1 m】 to see how to use it.
Emacs also has artist-mode. It lets you draw ASCII pictures with mouse.
+--------------+ -----------
| +---+ | --/ ------- \--
| |+--+--------+---+ -/ --/ \-- \-
| || | | | / / ----- \ \
| || | +-----+-+ | | | ( ) | |
+-++--+--+-----+ | | \ \ ----- / /
|| | +-------+ | -\ --\ /-- /-
|+--+------------+ --\ ------- /--
| | ----------- ●●●●
| | ●●●●●●● ●●●
+---+ ●●●●● ●●●●●●●●●●● ●●●● ●●
●●●●● ●●●●●●●●●● ●●●●●● ● ● ●
●●●●● ●●●●●●●●●●●●●●●● ●● ● ● ●● ●●
●●● ●●●● ●●●● ● ● ● ●
●●● ●●●● ●●●●●● ●● ●●● ● ●●
●●● ●●● ●●● ●●●● ●●● ●● ●
● ●●● ●●●●●●● ●●●●● ●●●●●●● ●● ●
● ●● ●●● ●●● ●●●● ●●●
●● ●● ●● ●●●●● ●●● ●●●●●●●●● ●●
●● ● ● ●●●● ●●● ●● ● ●
●● ● ●● ●●● ●● ● ● ●●●●● ●●
● ● ●●●●●● ● ● ● ●● ●●
●● ●● ●● ●● ● ● ●
●●●● ● ●●● ●● ●● ●● ●
●●●●●●●● ●●●●●●●●●● ●● ●● ● ●●
●●●●●●●● ●●● ●●● ●● ●● ●
●●●●●●●● ●● ●●●●●●●●● ●●● ● ●
●●●●●●● ● ●●● ●● ●●
●●● ●● ●●● ●● ●● ●●
●●● ●●● ●● ●●●●●●●● ●●
●●●●●●●●● ●● ●● ●●●
●●● ●● ●●●●●●●●●●●●●●●●●
● ●●●●●●●●●●
● ●●●●●●●●●●●●●●●●●●●●●●
●●●● ●●●
●●●●●●●●●●●●●●●●●●●●●●●●
artist-mode.You can see a video of this guy using it: Emacs Screencast: Artist Mode By Seong-Kook Shin. @ www.cinsk.org….