1

I already have the following table in a file elite_cyclists.txt

Name                Height  Weight  BMI
Nairo Quintana      167     59      21.2
Chris Froome        186     68      19.9
Robert Förstemann   174     90      29.7
Conor Dunne         204     88      21.1

hand-aligned using tab characters.

I would like to store the data in a file elite_cyclists.org within a table in org-mode.

I could:

  • copy the entries by hand,
  • write a macro that cleverly (or not) copies from position to cell (in C-x 2, say), or
  • insert the requisite ----, |, and + 's to make the table a semantic table, then copy to the .org file.

But all these seem like hacks.

Is there a mechanism for switching a .txt table to a org table?

Related
4
  • Org mode tables do not have boxes around each cell. You might be thinking of table.el tables? For the alignment, see my answer.
    – NickD
    Dec 13, 2022 at 19:11
  • @NickD When I specify a cell-width that's too narrow for the content of some cells, the text in those cells gets split to multiple lines. Also, as soon as I press C-c backquote, the org-mode editor inserts those row separators. Puzzling...
    – Sam7919
    Dec 13, 2022 at 19:14
  • Not in any Org mode that I'm familiar with: the cell expands to accommodate the length of the field (and TAB realigns everything) and C-c <backquote> is bound to org-table-edit-field. Are you sure you are not talking about table.el tables?
    – NickD
    Dec 13, 2022 at 19:23
  • See Built-in table editor in the Org mode manual and see table.el tables in the Emacs manual.
    – NickD
    Dec 13, 2022 at 19:29

5 Answers 5

3

Put the table into an org-buffer, highlight it to set the region and do M-x org-table-convert-region. That's all!

2
  • I am truly amazed. But still, I'm missing a detail. Could you look at the clarification question above?
    – Sam7919
    Dec 13, 2022 at 19:01
  • See @NickD's answer. Dec 13, 2022 at 20:11
3

EDIT: I missed the ... hand-aligned by TABs... part, and since I never use TAB characters in my files (except in Makefiles where they are significant), I misinterpreted the input. Nevertheless, the answer below might still be useful.


The function org-table-convert-region can be used to convert textual tabular material into an org table. Ideally, the textual material would have unambiguous field boundaries (e.g. commas or tabs in the case of CSV/TSV files), but the function can deal with somewhat regular ambiguous situations like the one you have: you have spaces separating the fields but you also have a single space between first and last name in the Names column; a quick look shows that if you can specify a separator of two or more spaces, that should be enough.

Fortunately the function can take a separator argument and as the doc string (C-h f org-table-convert-region) shows, you can specify a regular expression for it: " +" (there are two spaces in there) will match two or more spaces. However, you will have to evaluate the function non-interactively to be able to specify the separator.

Start by marking the region (C-SPC on one end and then move point to the other end) and then say:

M-: (org-table-convert-region (region-beginning) (region-end) "  +")

You will also have to add the hline that separates the header line from the body of the table by hand. But that's easily done: open a line and type |- followed by TAB to realign the table.


EDIT: BTW, you could deal with multiple TABs the same way as above. The separator regexp would have to be changed to "\t+" (i.e. one or more TABs) but that's all.

However, reformatting the original file to provide unambiguous field boundaries (e.g. a single TAB or a single comma) would make it a two-step operation as described by @FranBurstall's answer. Preprocessing the input to make it convenient for future transformations is a technique that's worth keeping in the back of your mind. And it does not have to be done in Emacs: a sed script can do it easily in your case, converting multiple TABs to a single one: sed -E 's/\t+/\t/g' (with GNU sed) should do it.

2
  • 1
    Here's hoping that once I learn org-mode I also will stop using TAB characters—other than for Makefiles.
    – Sam7919
    Dec 13, 2022 at 20:04
  • No, that's my peculiarity: lots of people use TABs in their files and lead long, productive lives :-)
    – NickD
    Dec 13, 2022 at 20:44
2

Caution

First a word of caution. If you call org-table-convert-region, you may not get what you want.

| Name   | Height     | Weight | BMI |      |
| Nairo  | Quintana   |    167 |  59 | 21.2 |
| Chris  | Froome     |    186 |  68 | 19.9 |
| Robert | Förstemann |    174 |  90 | 29.7 |
| Conor  | Dunne      |    204 |  88 | 21.1 |

Ensure single TABs

Hence you need first to make sure that the table has one and exactly one TAB character between cells.

Name    Height  Weight  BMI
Nairo Quintana  167 59  21.2
Chris Froome    186 68  19.9
Robert Förstemann   174 90  29.7
Conor Dunne 204 88  21.1

This can be easily done by, for example, search-replacing TAB TAB with TAB, in multiple passes until it fails at the start (although there is surely a better way via isearch-forward-regexp).

Run `org-table-convert-region`

It's now sufficient to copy-paste the data to a .org file, mark the region (with or without the final end-of-line) and run M-x org-table-convert-region. The result will be:

| Name              | Height | Weight |  BMI |
| Nairo Quintana    |    167 |     59 | 21.2 |
| Chris Froome      |    186 |     68 | 19.9 |
| Robert Förstemann |    174 |     90 | 29.7 |
| Conor Dunne       |    204 |     88 | 21.1 |
Credit: Fran Burstall
1

I would probably use M-x untabify to convert the tabs into spaces, then select the region and do C-u 2 C-c | to convert the table to an org-mode table. The numerical prefix specifies that at least 2 spaces are needed to denote a new column, which will avoid breaking the names and misaligning the numerical columns.

1

This is an answer for those who enjoy using Emacs's rectangle commands.

Leave multiple tabs where they are

Name              Height  <TAB>Weight        BMI
Nairo Quintana    167<TAB><TAB>59            21.2
Chris Froome      186  (etc)   68            19.9
Robert Förstemann 174          90            29.7
Conor Dunne       204          88            21.1

and run `string-rectangle` C-xrt multiple times to insert the requisite `|` marks.

| Name              | Height        | Weight    | BMI  | 
| Nairo Quintana    | 167           | 59        | 21.2 | 
| Chris Froome      | 186           | 68        | 19.9 | 
| Robert Förstemann | 174           | 90        | 29.7 | 
| Conor Dunne       | 204           | 88        | 21.1 | 

This will require manually (and perhaps laboriously, YMMV) re-aligning columns as you proceed.

Now switch to `text-mode` and back to `org-mode`. The table will look corrupted, but it will be recognized as a table.

| Name                      | Height                | Weight        | BMI  | 
| Nairo Quintana    | 167                   | 59            | 21.2 | 
| Chris Froome              | 186                   | 68            | 19.9 | 
| Robert Förstemann | 174                   | 90            | 29.7 | 
| Conor Dunne               | 204                   | 88            | 21.1 | 

Now simply press Tab in any cell to adjust the cell alignment.

| Name              | Height | Weight |  BMI |
| Nairo Quintana    |    167 |     59 | 21.2 |
| Chris Froome      |    186 |     68 | 19.9 |
| Robert Förstemann |    174 |     90 | 29.7 |
| Conor Dunne       |    204 |     88 | 21.1 |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.