5

windows 10, emacs 26.1

Supose I have the following text:

OwnerKey    OrganizationName    GUID
111192    Infocom    41EA23423423423423423C8-62A61A1532C5
345423   TV    A3B8F234234234234239FA5-4D71242359EE
123423     TV    A3B823423423424234-234234234322659EE

I need to create something like this:

| OwnerKey | OrganizationName | GUID                                 |
|----------+------------------+--------------------------------------|
|   111192 | Infocom          | 41EA23423423423423423C8-62A61A1532C5 |
|   345423 | TV               | A3B8F234234234234239FA5-4D71242359EE |
|   123423 | TV               | A3B823423423424234-234234234322659EE |

How I can do this quickly. I don't want to do this manually line by line.

Stefan
  • 26,154
  • 3
  • 46
  • 84
a_subscriber
  • 3,854
  • 1
  • 17
  • 47

3 Answers3

10

You find the Tbl menu at the right side of the Org menu. Mark the region you want to convert to an org-table and click on the menu item Tbl -> Convert Region.

The result is a table without header:

| OwnerKey | OrganizationName | GUID                                 |
|   111192 | Infocom          | 41EA23423423423423423C8-62A61A1532C5 |
|   345423 | TV               | A3B8F234234234234239FA5-4D71242359EE |
|   123423 | TV               | A3B823423423424234-234234234322659EE |

Adding the missing header is easy. Place point behind the first line Press RET for a newline and input the two characters |- and a tab. This adjusts the underlining of the header automagically.


You can use the minor mode orgtbl-mode also in buffers that are not in org-mode but in other major modes like text-mode.
Just call M-x orgtbl-mode RET and you get a new menu OrgTbl in the menu bar.
There you find the menu item Create or Convert bound to the key C-|. That works very similar to the above Convert Region.

It may be that you cannot insert the newline for the header underlining by just typing RET behind the first line. In that case type C-q C-j instead of RET to insert a newline.
Everything else works like for Convert Region above.

The really nice thing is that you even can insert #+TBLFM: lines below the table. Those lines work like in org-mode when you type C-c C-c on them.

Tobias
  • 32,569
  • 1
  • 34
  • 75
7

Select the data.

M-x table-capture

Now it will ask questions like what is the delimiter etc.

For column delimiter hit space key

Row delimiter is RET, but we can't use it because it's the confirmation key, so you need to hit

C-q C-j

Other options you can probably leave at their defaults.

Stefan
  • 26,154
  • 3
  • 46
  • 84
music
  • 326
  • 3
  • 4
3

While I really liked the answers here, I personally found that both table-capture and orgtbl-create-or-convert-from-region did not suit my situation well. So here's another option:

Select the text and use either M-x replace-regexp or M-x query-replace-regexp for each of the following:

  1. ^ -> | (|space)
  2. -> | (three spaces to space|space)
  3. $ -> | (space|)

Finally, insert a line after the heading and type |-TAB to create a horizontal rule and align the table.

James Brusey
  • 169
  • 1
  • 5