Emacs: Edit Column Text, Align Text, ASCII-Diagram

Master emacs+lisp, benefit for life. Testimonials. Thank you for support.
, , …,

This page shows you how to {copy, cut, insert} a rectangular region, and how to draw ASCII-diagram, align text.

kill-rectangle & replace-rectangle

The most useful rectangle commands are:

CommandKeyPurpose
kill-rectangleCtrl+x r kDelete selected column of text.
replace-rectangleCtrl+x r tReplace text in a selected column. (This is alias to string-rectangle)

Here's a example of how to use it.

..............
..1xxx..oooo..
..xxxx..oooo..
..xxxx..oooo..
..xxxx..oooo..
..xxxx..oooo..
..xxxx..oooo..
..xxxx..oooo..
..xxxx..oooo..
..xxxx..oooo..
..xxx2..oooo..
..............
  1. Put your cursor before the “1”
  2. call set-mark-commandCtrl+Space
  3. move cursor to end of “2”
  4. call kill-rectangle

Paste Rectangle

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

Inserting Vertical Column of Numbers

commandKeyPurpose
rectangle-number-linesCtrl+x r NInsert 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, call universal-argumentCtrl+u】 first.

To insert letters instead, see: Emacs Lisp: Insert Alphabets A to Z.

Less Used Rectangle Commands

Here's other rectangle commands.

commandKeyPurpose
delete-rectangleCtrl+x r dsimilar to kill-rectangle, but doesn't save to a special clipboard.
string-insert-rectanglesimilar to string-rectangle, but doesn't replace the rectangle text.
clear-rectangleCtrl+x r creplace with space
delete-whitespace-rectangleDelete all whitespace following starting corner. (this is alias to close-rectangle)
delimit-columns-rectangle
open-rectangleCtrl+x r oInsert space to the rectangle. (shifting text to the right)

Copy Column of Text to Clipboard

If you want to copy rectangle of text to standard OS clipboard, see: Emacs Lisp: Copy Column of Text to Clipboard.

Text Column Commands in cua-mode

cua-mode has a nice feature that makes emacs rectangle commands much easier to use, because it highlights just the rectangle, and user can just use normal copy/paste keys to manipulate the rect.

  1. Call cua-mode to turn it on.
  2. Ctrl+Enter ↵】 to start selecting rectangle.
  3. you can start typing to insert text to the whole column, or press 【⌫ Backspace】 to delete to the left every char on the left boundary, or press 【⌦ Delete】 to delete the whole rectangle, or use any of {Cut 【Ctrl+x】, Copy 【Ctrl+c】, Paste 【Ctrl+v】}.
  4. To cancel selection, press 【Ctrl+Enter ↵】 again.

(thanks to Markus Schütz)

Align Text

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

picture-mode

Emacs also has a picture-mode, designed for drawing ASCII diagrams.

Open a new file. Then, call picture-mode, then call describe-modeF1 m】 to see how to use it.

Here's a examples of ASCII diagram you can play with.

  _____________________________________________
 |                                             |
 |          Sequence                           |
 |  ______   ________________________________  |
 | |      | |                                | |
 | | List | |             Array              | |
 | |      | |    ________       ________     | |
 | |______| |   |        |     |        |    | |
 |          |   | Vector |     | String |    | |
 |          |   |________|     |________|    | |
 |          |  ____________   _____________  | |
 |          | |            | |             | | |
 |          | | Char-table | | Bool-vector | | |
 |          | |____________| |_____________| | |
 |          |________________________________| |
 |_____________________________________________|
(info "(elisp) Sequences Arrays Vectors")

artist-mode

Emacs also has artist-mode. It lets you draw ASCII pictures with mouse.

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

Here's video demonstrating drawing a diagram using artist-mode: 〔Emacs Screencast: Artist Mode By Seong-Kook Shin. @ http://www.cinsk.org/emacs/emacs-artist.html〕.

Like what you read?
Buy Xah Emacs Tutorial
or share some
blog comments powered by Disqus