working with tables data in emacs

perm url with updates: http://xahlee.org/emacs/emacs_table.html

Working With Tables In Emacs

Xah Lee, 2009-08-20

This page is a tutorial, showing how use emacs's table feature, to create or manipulate tabular data. You can export this table to HTML or LaTex formats.

The advantage of using emacs table feature is that you can visually add/delete columns and rows, then export it to HTML or LaTex.

Note that this feature is not very robust. If your table data is just few rows and columns, with simple text or numbers, then emacs table feature is very useful. But if you work with importing/exporting actual spread sheets or address books, emacs table feature does not handle it well.

Creating A Table

Creating A New Empty Table

To create a table, type “Alt+x table-insert”.

It'll ask you these questions:

Number of columns
Number of rows
Cell width
Cell height

You can just press Enter key to accept the default. You can change the values for each later. The result will look like this:

+-----+-----+-----+
|     |     |     |
+-----+-----+-----+
|     |     |     |
+-----+-----+-----+
|     |     |     |
+-----+-----+-----+

Press Tab or Shift+Tab to go to neighbor cells.

Creating A Table From Delimiter Separated Values

If you already have a table data, such as Comma Separated Values (CSV) from spreadsheets, or even from a existing HTML table, you can create a table from that.

Let's say you have this comma separated values:

,A,B,C
1,1a,1b,1c
2,2a,2b,2c
3,3a,3b,3c

Select the whole text, then type “Alt+x table-capture”. It'll ask you these questions:

  • Column delimiter regexp
  • Row delimiter regexp
  • Justify (left,center,right)
  • Minimum cell width

To enter a tab character (ASCII 9) in the minibuffer prompt, type “Ctrl+q Ctrl+Tab”. To enter a newline literally, type “Ctrl+q Ctrl+j”.

Note: Emacs always represent newlines in buffer using the unix convention of the Line Feed (LF) char (ASCII 10). To enter the LF literally, you type “Ctrl+q Ctrl+j”. You don't have to worry what operating system convention for newline is used in your file.

Using Table Menu

emacs table

Use the Menu!

Once you have created a table, you can use the menu to do things. The menu “Table” will appear when your cursor is on a table cell.

If you prefer using commands, you can type “Alt+x table-” then press “Tab” to see a list of all commands.

Some of the most useful are:

table-insert-row
table-insert-column
table-delete-row
table-delete-column

table-generate-source      output to HTML, LaTeX, CALS
table-release              output to sepace separated values & lines

Generating Output

To generate output for your table, pull the menu “Table‣table-generate-source” or type “Alt+x table-generate-source”. Then, you can choose one of html, latex, or cals (a XML based one).

To output to space delimited values, use call table-release.

(info "(emacs) Text Based Tables")

Popular posts from this blog

11 Years of Writing About Emacs

does md5 creates more randomness?

Google Code shutting down, future of ErgoEmacs