I have some org files that I generated from a program I wrote. My program is not intelligent enough to parse the tables in these files "correctly". For example, one of my org files may look like

#+Title: hello world

* section 1

here is an unaligned table

|this |is | my |
| first| unaligned| table |

* section 2

here are two more unaligned tables

| this | is |my |
|second| unaligned |     table|

|yet|another| unaligned|
|table| to |alter|

I'd like to be able to automatically align all the tables in these files from the command line. Thus running such a command on my above file would yield

#+Title: hello world

* section 1

here is an unaligned table

| this  | is        | my    |
| first | unaligned | table |

* section 2

here are two more unaligned tables

| this   | is        | my    |
| second | unaligned | table |

| yet   | another | unaligned |
| table | to      | alter     |

Does such a command exist?

I'm aware that setting org-startup-align-all-tables to t will automatically align every table upon visiting the file but I'd like to align the tables without actually opening the file.


It seems that there is no command to do what you want, but in emacs we can have our way

I have wrote a small shell script based on the tangle example in the emacs manual

# -*- mode: shell-script -*-
# tangle files with org-mode

# wrap each argument in the code required to call tangle on it
for i in $@; do
FILES="$FILES \"$i\""

emacs -Q --batch \
--eval "(progn
     (require 'org)(require 'org-table)
     (mapc (lambda (file)
            (find-file (expand-file-name file \"$DIR\"))
            (org-table-map-tables 'org-table-align)
            (write-file file nil)
            (kill-buffer)) '($FILES)))"

Be aware that the script overwrites the original file

|improve this answer|||||
  • How does that align tables as op wants? – Emacs User Sep 14 '15 at 14:43
  • To my understanding OP has the files with unaligned tables, and wants to align them as Org would do when opening a file. I tested the script and it did that PLease pay attention to the "org-table-map" line – Joafigue Sep 14 '15 at 14:46
  • Well, this works perfectly! Thanks for the help! – Brian Fitzpatrick Sep 14 '15 at 18:46
  • @Joafigue I wonder if this script could be altered to also update all the formulas in the tables... Would that be an easy alteration or should I ask a separate question? – Brian Fitzpatrick Sep 15 '15 at 6:14
  • Ahh.. figured it out. Replacing 'org-table-align with 'org-table-iterate does the trick. – Brian Fitzpatrick Sep 15 '15 at 6:54

Does such a command exist?

No, but you can easily mimic in a variety of ways. The easiest approach seems an adaptation of org-mode's built-in column alignment overrides <10> at the top of each column. For example:

| <8>    |    <11>   |   <12>    |
| this   | is        | my        |
| second | unaligned | table     |

your second table

| <8>    |    <11>   |   <12>    |
| yet    | another   | unaligned |
| table  | to        | alter     |

where <8>, <11> and <12> are arbitrary column widths that you specify for each column of each table, overriding the automatic alignment. This will have the effect of aligning all the columns and the tables.

I can confirm I've used this feature extensively while dumping to html.


To print in orgmode, you must invoke the export to text feature:

C-c C-e t a

There are other options to save to file, save to a new buffer, etc. If you want to export from the command line, use one like this:

emacs foo.org --batch -f org-ascii-export-to-ascii --kill

The manual has more details about batch execution.

|improve this answer|||||
  • This doesn't seem to to accomplish what I'd like. Issuing cat foo.org on my file still yields an unaligned table. – Brian Fitzpatrick Sep 14 '15 at 2:25
  • cat won't work, see the updated addendum in the answer. – Emacs User Sep 14 '15 at 3:44

In an org file I have several named code blocks, some of them generate (#+RESULTS) named tables. Whenever I want to regenerate the tables in the file, I run org-babel-execute-buffer, and then I end up in a situation pretty much similar to yours, that is with unaligned tabled.

Well, I should say I "would" end up in such situation, but in fact the latest code block in the file does the alignment for me:

#+BEGIN_SRC emacs-lisp :results output silent
  (org-table-map-tables 'org-table-align)

This block applies in fact what is used in the selected answer. By including this block at the end, after generating all the tables they are automatically re-aligned.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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