Emacs: manipulate Column Text, string-rectangle, ASCII-Art
Perm url with updates: http://xahlee.org/emacs/emacs_string-rectangle_ascii-art.html
Emacs: Manipulate Column Text, string-rectangle, ASCII-Art
Xah Lee, 2011-09-14
Some tips on 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 a week.
|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 lines. This is alias to “string-rectangle”.|
I use the above so often that in the past 2 years i gave them a shortcut such as 【Ctrl+8】 and 【Ctrl+9】.
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:
|yank-rectangle||【Ctrl+x r y】||paste a column of text (after you used “killed-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.
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
Less Used Rectangle Commands
Here's others rectangle commands, but i've never used them:
|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.|
|open-rectangle||【Ctrl+x r o】||Blank out the region-rectangle, shifting tex|
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” 【Ctrl+h 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 (2009-05-25) 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:
- The Harm of Hard-wrapping Lines
- Tabs versus Spaces in Source Code
- Unix, RFC, Line Truncation
- A Text Editor Feature: Extend Selection by Semantic Unit
- A Simple Lisp Code Formatter
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+;】