Xah Lee, , …,
This page shows how to {copy, cut, insert} a column of text in emacs.
In my 12 years of using emacs daily, the most useful to me are these two. I use them few times a week.
| Command | Key | Purpose |
|---|---|---|
| kill-rectangle | 【Ctrl+x r k】 | Delete a column block of text (and put it in a special clipboard) |
| replace-rectangle | 【Ctrl+x r t】 | Insert text in all selected lines starting at cursor's position. 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 text somewhere. Try to swap the “x” block and “o” block in the above sample text.
These “rectangle” commands is designed for drawing ASCII diagrams. Here's some examples of ASCII diagram.
_____________________________________________ | | | Sequence | | ______ ________________________________ | | | | | | | | | List | | Array | | | | | | ________ ________ | | | |______| | | | | | | | | | | Vector | | String | | | | | |________| |________| | | | | ____________ _____________ | | | | | | | | | | | | | Char-table | | Bool-vector | | | | | |____________| |_____________| | | | |________________________________| | |_____________________________________________|
--------------- ---------------- ------------------- | car | cdr | | car | cdr | | car | cdr | | rose | o-------->| violet | o-------->| buttercup | nil | | | | | | | | | | --------------- ---------------- -------------------
I almost never do ASCII diagram, yet, i still find kill-rectangle and replace-rectangle useful almost everyday when editing source code.
Emacs has another function that's helpful for alignment, the align-regexp function. 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
Here's others rectangle commands, but i've never used them:
| command | Key | Purpose |
|---|---|---|
| delete-rectangle | 【Ctrl+x r d】 | like kill-rectangle, but doesn't save in a special kill-ring |
| clear-rectangle | 【Ctrl+x r c】 | replace with space |
| string-insert-rectangle | ◇ | like string-rectangle, but doesn't replace existing content |
| close-rectangle | ◇ | remove white places |
| delete-whitespace-rectangle | ◇ | Delete all whitespace following a specified column in each line. |
| delimit-columns-rectangle | ◇ | ◇ |
| open-rectangle | 【Ctrl+x r o】 | Blank out the region-rectangle, shifting text. |
Emacs also has a picture-mode, designed for drawing ASCII diagrams. I've never used it.
Open a new file. (in ErgoEmacs, 【Ctrl+n】. In GNU Emacs, call switch-to-buffer 【Alt+x b】, then give a random name.) Then, call picture-mode, then call describe-mode 【F1 m】 to see how to use it.
Emacs also has a artist-mode. It's a mode that lets you draw ASCII pictures with the mouse.
Open a new file. Now, you can draw with your mouse. Hold right button to erase. Middle click to switch to rectangle, ellipse, and other tools.
+--------------+ -----------
| +---+ | --/ ------- \--
| |+--+--------+---+ -/ --/ \-- \-
| || | | | / / ----- \ \
| || | +-----+-+ | | | ( ) | |
+-++--+--+-----+ | | \ \ ----- / /
|| | +-------+ | -\ --\ /-- /-
|+--+------------+ --\ ------- /--
| | ----------- ●●●●
| | ●●●●●●● ●●●
+---+ ●●●●● ●●●●●●●●●●● ●●●● ●●
●●●●● ●●●●●●●●●● ●●●●●● ● ● ●
●●●●● ●●●●●●●●●●●●●●●● ●● ● ● ●● ●●
●●● ●●●● ●●●● ● ● ● ●
●●● ●●●● ●●●●●● ●● ●●● ● ●●
●●● ●●● ●●● ●●●● ●●● ●● ●
● ●●● ●●●●●●● ●●●●● ●●●●●●● ●● ●
● ●● ●●● ●●● ●●●● ●●●
●● ●● ●● ●●●●● ●●● ●●●●●●●●● ●●
●● ● ● ●●●● ●●● ●● ● ●
●● ● ●● ●●● ●● ● ● ●●●●● ●●
● ● ●●●●●● ● ● ● ●● ●●
●● ●● ●● ●● ● ● ●
●●●● ● ●●● ●● ●● ●● ●
●●●●●●●● ●●●●●●●●●● ●● ●● ● ●●
●●●●●●●● ●●● ●●● ●● ●● ●
●●●●●●●● ●● ●●●●●●●●● ●●● ● ●
●●●●●●● ● ●●● ●● ●●
●●● ●● ●●● ●● ●● ●●
●●● ●●● ●● ●●●●●●●● ●●
●●●●●●●●● ●● ●● ●●●
●●● ●● ●●●●●●●●●●●●●●●●●
● ●●●●●●●●●●
● ●●●●●●●●●●●●●●●●●●●●●●
●●●● ●●●
●●●●●●●●●●●●●●●●●●●●●●●●
You can see a video of this guy using it: Emacs Screencast: Artist Mode By Seong-Kook Shin. @ Source www.cinsk.org.
I should warn that you should not get into a habit of using ASCII art in comments, such as drawing boxes. Because, it is a form of hard-formatting. As such, it is not flexible and creates all sorts of problems. See:
For the same reason, i never liked any comment alignment in my source code, and i am annoyed that emacs by default aligns your comment when you call comment-dwim 【Alt+;】