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.

CommandKeyPurpose
kill-rectangleCtrl+x r kDelete a column block of text (and put it in a special clipboard)
replace-rectangleCtrl+x r tInsert 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:

CommandKeyPurpose
yank-rectangleCtrl+x r ypaste 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.

  _____________________________________________
 |                                             |
 |          Sequence                           |
 |  ______   ________________________________  |
 | |      | |                                | |
 | | List | |             Array              | |
 | |      | |    ________       ________     | |
 | |______| |   |        |     |        |    | |
 |          |   | Vector |     | String |    | |
 |          |   |________|     |________|    | |
 |          |  ____________   _____________  | |
 |          | |            | |             | | |
 |          | | Char-table | | Bool-vector | | |
 |          | |____________| |_____________| | |
 |          |________________________________| |
 |_____________________________________________|
(info "(elisp) Sequences Arrays Vectors")
  ---------------       ----------------       -------------------
 | car   | cdr   |     | car    | cdr   |     | car       | cdr   |
 | rose  |   o-------->| violet |   o-------->| buttercup |  nil  |
 |       |       |     |        |       |     |           |       |
  ---------------       ----------------       -------------------
(info "(elisp) Box Diagrams")

I almost never do ASCII diagram, yet, i still find “kill-rectangle” and “replace-rectangle” useful almost everyday when editing source code.

align-regexp

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:

commandKeyPurpose
delete-rectangleCtrl+x r dlike “kill-rectangle”, but doesn't save in a special kill-ring
clear-rectangleCtrl+x r creplace with space
string-insert-rectanglelike “string-rectangle”, but doesn't replace existing content
close-rectangleremove white places
delete-whitespace-rectangleDelete all whitespace following a specified column in each line.
delimit-columns-rectangle
open-rectangleCtrl+x r oBlank out the region-rectangle, shifting tex

picture-mode

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.

artist-mode

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.

    +--------------+             -----------
    | +---+        |          --/   ------- \--
    | |+--+--------+---+    -/   --/       \-- \-
    | ||  |        |   |   /    /    -----    \  \
    | ||  |  +-----+-+ |   |    |   (     )   |  |
    +-++--+--+-----+ | |   \    \    -----    /  /
      ||  |  +-------+ |    -\   --\       /-- /-
      |+--+------------+      --\   ------- /--
      |   |                      -----------           ●●●●
      |   |                                     ●●●●●●●   ●●●
      +---+             ●●●●●         ●●●●●●●●●●●   ●●●●    ●●
                    ●●●●●    ●●●●●●●●●●    ●●●●●●   ●    ●   ●
                ●●●●●      ●●●●●●●●●●●●●●●●     ●●  ● ●  ●●  ●●
              ●●●       ●●●●              ●●●●   ●    ●   ●   ●
            ●●●      ●●●●           ●●●●●●      ●●  ●●●   ●  ●●
          ●●●      ●●●         ●●●       ●●●●     ●●●    ●●  ●
          ●      ●●●    ●●●●●●●  ●●●●●      ●●●●●●●     ●●   ●
         ●      ●●    ●●●            ●●●             ●●●●  ●●●
        ●●     ●●    ●●       ●●●●●    ●●●   ●●●●●●●●●    ●●
       ●●      ●    ●     ●●●●    ●●●    ●●   ●           ●
      ●●      ●    ●●   ●●●        ●●     ●   ●   ●●●●●   ●●
                   ●    ●     ●●●●●●      ●   ●   ●   ●●   ●●
                   ●●        ●●         ●●   ●●   ●    ●    ●
                    ●●●●     ●        ●●●   ●●   ●●   ●●    ●
                  ●●●●●●●●   ●●●●●●●●●●    ●●   ●●    ●    ●●
                  ●●●●●●●●    ●●●         ●●●  ●●    ●●    ●
                  ●●●●●●●●   ●● ●●●●●●●●●    ●●●     ●     ●
                   ●●●●●●●  ●           ●●●         ●●    ●●
                      ●●●  ●●     ●●●     ●●       ●●    ●●
                         ●●●    ●●● ●●     ●●●●●●●●     ●●
                 ●●●●●●●●●     ●●    ●●               ●●●
               ●●●            ●●      ●●●●●●●●●●●●●●●●●
               ●     ●●●●●●●●●●
               ●      ●●●●●●●●●●●●●●●●●●●●●●
               ●●●●                      ●●●
                  ●●●●●●●●●●●●●●●●●●●●●●●●
Some doodle with “artist-mode”.

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:

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+;

Popular posts from this blog

Browser User Agent Strings 2012

11 Years of Writing About Emacs

does md5 creates more randomness?