Emacs Advent Calendar 22: Tables

Tue 2015-12-22

Until now we have only looked at vanilla Emacs features. These also include commands for editing tables, but tables from Org Mode (which is distributed alongside emacs) are much more versatile.

To use Org mode you may have to add the following two lines to your emacs initialization file:

(add-to-list 'load-path "/usr/share/emacs/site-lisp/org/")
(add-to-list 'load-path "/usr/share/emacs/site-lisp/org_contrib/")

But the selling feature is that you will not need to use Org mode as a major mode, but can activate orgtbl-mode as a minor mode alongside whatever major mode you are currently in. This enables you to edit, convert and export tables with full Org mode power.

Org Tables look like this

| Name         | Answer | Pass |
|--------------+--------+------|
| Zaphod       | Foo    | nope |
| Arthur       | Tea    | nope |
| Ford         | 43     | nope |
| Deep Thought | 42     | yup  |

Each time you press <TAB>, <RET> or C-c C-c inside a table it will be re-aligned automatically. To create the example table, just enter the following in your buffer and press <TAB>

|Name|Answer|Pass|
|-

If you already have something table-like in your buffer (fields separated by commas, spaces or tabs), activate the mark around it and press C-c | to make Org try recognizing it intelligently.

Moving around in a table is accomplished with <TAB>, S-<TAB> and <RET>. Pressing <TAB> in the last field will open a new row at the bottom of the table. The keys M-a and M-e move point to the beginning or end of the current field.

To move the current column or row, press M and one of the arrow keys. M-S-<right> and M-S-<down> open up a new column to the left/above the current one.

If you want to sort the current column, press C-c ^.

Org can export your table to various formats. With point inside the table run M-x org-table-export and give a filename to save the exported table to. Org will decide the export format based on the file extension, it supports (among others) csv, LaTeX and html.

\begin{tabular}{lll}
  Name & Answer & Pass \\
  \hline
  Zaphod & Foo & nope \\
  Arthur & Tea & nope \\
  Ford & 43 & nope \\
  Deep Thought & 42 & yup \\
\end{tabular}

If you need the table in Emacs' own table.el syntax, run M-x org-table-convert with point anywhere in the table. Executing that a second time will convert the table back to Org syntax.

+--------------+--------+------+
| Name         | Answer | Pass |
+--------------+--------+------+
| Zaphod       | Foo    | nope |
+--------------+--------+------+
| Arthur       | Tea    | nope |
+--------------+--------+------+
| Ford         | 43     | nope |
+--------------+--------+------+
| Deep Thought | 42     | yup  |
+--------------+--------+------+

Org tables have a huge number of other features (e.g. spreadsheet, plotting) for which I kindly refer you to chapter 3 (Tables) of the Org Mode Manual.

Tags:

This text by Ludger Sandig is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.