I am writing a literate program in Org mode, and exporting the literate document to html file after tangling.

However, after making any change to the Org file, I often have to do the export (Using C-c C-e h h) and tangling (C-c C-v t) separately. This gets tedious when making lots of local changes to see the effect on the code's run.

Is there a way to add a command to emacs via Elisp, to do both simultaneously. I'd like to bind this action to the simultaneous key-pressesAlt+F2

2 Answers 2


I don't think Org has an option to tangle-on-export. Bu Org can evaluate code blocks on export, and one of those can do the tangling for you. I don't have the elisp skills to whip this out easily, but you'll want an elisp babel block that steps through all of the other code blocks (or a list of specific named blocks) and tangles them (or tangles them if :tangle is y and the cache is outdated, etc.)


Inside the current buffer

The simplest way is to create a function that does those two things for you.

When you press (C-c C-e h h) the function org-html-export-to-html is run in the current buffer.

When you press (C-c C-v t) the function org-babel-tangle is run in the current buffer.

Therefor you can use the following function to do both for you:

(defun export-tangle ()
  "Shortcut for exporting and tangling the current org-mode buffer."

You can then bind this function to Alt+F2 using:

 (lambda ()
   (local-set-key (kbd "M-<f2>") 'export-tangle)))

In a script

Sometimes you want to do this on files instead of the currently open buffer.

You can then create a similar function that opens the file for you and runs the functions for you:

(defun export-tangle-file (filepath)
  "Shortcut for exporting and tangling the org-mode file at `filepath'."
  (find-file (expand-file-name filepath))

You can then use that function in a script for example. Like this:

(export-tangle-file "article.org")

For all files in a directory

You can even setup org-mode to publish and tangle a whole directory tree full of org-mode files for you using the setting org-publish-project-alist.

Some people use this to write blogs in org-mode and then publish the whole blog using (C-c C-e P p).

With org-publish-project-alist you create components that does different types of publishing.

Here's an example of how you can set up copying static assets, publishing org-mode articles and tangling org-mode articles:

(setq org-publish-project-alist
         :base-directory "~/blog/articles/assets/"
         :base-extension "css\\|js\\|png\\|jpg\\|gif\\|pdf"
         :publishing-directory "~/blog/output/assets/"
         :recursive t
         :publishing-function org-publish-attachment)
         :base-directory "~/blog/articles/"
         :publishing-directory "~/blog/output/"
         :recursive t
         :publishing-function org-html-publish-to-html)
         :base-directory "~/blog/articles/"
         :publishing-directory "~/blog/output/tangles/"
         :recursive t
         :publishing-function org-babel-tangle-publish)
         :components ("assets" "tangles" "articles"))))

Note: org-table-tangle-publish will put all tangles in the root of the publish-directory.

So if you have setup tangling with a directory structure, it will be flattened.

I also encountered an error when the directory publish-directory didn't exist. After creating the directory it worked.

  • I do not know if there was a behavior change since your post but in my case org-table-tangle-publish respected the directory structure. Jun 23, 2017 at 9:01

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.