14

Is anyone aware of an emacs mode or function that will reformat a buffer holding a delimited file such that each delimiter (e.g. tab) defines a "column" with the width of each column set to the longest entry?

I can reset the tab-width variable but that makes all columns equal width. I would much prefer variable-width columns if possible (like how a spreadsheet program like excel would display such a file after resizing the columns).

Alex Stoddard
  • 8,244
  • 4
  • 41
  • 61
  • For cross-reference purposes, the following question demonstrates two other solutions to this question, using (a) Emacs' table.el library, and (b) SES (Simple Emacs Spreadsheet), which provides a proper spreadsheet application: http://stackoverflow.com/questions/6273647/add-remove-column-spreadsheet-features-in-emacs – phils Jun 12 '11 at 22:19

3 Answers3

14

Check out Org mode's table editor.

huaiyuan
  • 26,129
  • 5
  • 57
  • 63
  • I had trouble connecting to http://orgmode.org/. An old copy of the manual can also be found here: http://www.gnu.org/software/emacs/manual/html_node/org/Built_002din-table-editor.html – Michel de Ruiter Dec 14 '10 at 10:36
  • org-mode is very good, but very slow on large files: http://lists.gnu.org/archive/html/emacs-orgmode/2013-08/msg00132.html So if your file is more than a few hundred lines it doesn't run very well. – Björn Lindqvist Jan 22 '14 at 10:04
5

Org-mode as suggested by huaiyuan did just what I wanted.

To give an example to others, after I installed org-mode I found the following to work well.

Open a new file in org-mode C-x C-f table.org and then M-x org-table-import to import the delimited file, and you're done.

The C-| command didn't work for me to convert pre-existing buffer contents into a table, it just inserted a new table, but I don't know org-mode yet.

Alex Stoddard
  • 8,244
  • 4
  • 41
  • 61
3

Let's assume your delimiter is TAB. Mark the whole buffer, then:

C-u M-x align-regexp \(\s-*\)TAB RET RET RET y

Where TAB in there is an actual tab char. See my similar answer here:

how to align arguments to functions in emacs?

Community
  • 1
  • 1
scottfrazer
  • 17,079
  • 4
  • 51
  • 49
  • Looks like you meant: C-u M-x align-regexp RET \(\s-*\)TAB RET RET RET y That works in emacs 23, but seems to give an error "not enough arguments for format string" in emacs 21 (which is still present on some systems that I use) . To be really picky it also expands all the columns by a tab stop, but I guess a global replace would take care of that. – Alex Stoddard Nov 12 '09 at 19:03