I could not find any header args to do this (I thought that :dir
would be the one, but it does not make any difference in tangling). But you can use the post-tangle hook, org-babel-post-tangle-hook
, to manipulate the tangled files after they are produced. Here is a test file:
#+PROPERTY: dest ./tangled/
* Link
* TODO foo
#+BEGIN_SRC asy :tangle test.asy
write("Hello World!");
#+END_SRC
#+BEGIN_SRC asy :tangle test2.asy
write("Good-bye World!");
#+END_SRC
* Code :noexport:
#+begin_src emacs-lisp
(setq ndk/tangle-dir (org-entry-get nil "dest" t))
(defun ndk/org-babel-tangle-rename ()
(let ((tangledir ndk/tangle-dir)
(tanglefile (buffer-file-name)))
(rename-file tanglefile tangledir t)))
(add-hook 'org-babel-post-tangle-hook #'ndk/org-babel-tangle-rename)
#+end_src
The code consists of three things:
the setting of a global variable ndk/tangle-dir
to the directory path where you want the tangled files to end up. You could hardwire this into the function, but I made it a bit more flexible by defining a global #+PROPERTY
called dest
whose value is the path (N.B. The path MUST end with a slash). We retrieve the value of the dest
property with org-entry-get
and set ndk/tangle-dir
to that value.
a function that will be called through the hook on every file that is tangled. The function is run in a buffer that contains the tangled output. We retrieve the file name from the buffer, and then we call rename-file
to move that file to the destination directory. The t
argument means it's OK to move it there even if a file with the same name exists in that directory already.
adding the function above to the org-babel-post-tangle-hook
.
If you create an Org mode file with the contents above, and create the destination directory, you should be able to open the Org mode file, C-c C-c
on the source code block, C-c C-v C-t
to tangle the two blocks in the foo
section and then check out the contents of the destination directory: the two tangled files should be there.
You probably want to remove the function from the hook afterwards, otherwise all files you tangle will end up in that directory:
(remove-hook 'org-babel-post-tangle-hook #'ndk/org-babel-tangle-rename)
For another example of the use of this hook, see this recent question. As you can see, it's pretty flexible!