8

I'm generating multiple files in one .org file, with multiple source code blocks. For example:

#+begin_src rst :tangle file1.rst :noweb yes
<<file1>>
#+end_src

#+begin_src rst :tangle file2.rst :noweb yes
<<file2>>
#+end_src

Is there any way to only tangle one specific code block without changing the header options?

I'm generating these files for Sphinx and to reduce the compilation time I want to tangle only the file I'm currently working on.

It would be great if there is a command such as org-babel-tangle-current-block - is there any way to do this?

(This is a related question to Orgmode: how to filter the blocks to be tangle?)

BinaryButterfly
  • 18,137
  • 13
  • 50
  • 91
joon
  • 3,899
  • 1
  • 40
  • 53

3 Answers3

13

That is possible by calling org-babel-tangle with a prefix argument.

So C-u C-c C-v t should do what you want.

From the docstring:

With one universal prefix argument, only tangle the block at point. When two universal prefix arguments, only tangle blocks for the tangle file of the block at point.

Andreas
  • 1,106
  • 9
  • 26
8

Just in case that someone is interested in binding a key to this - my init file includes the following:

(defun org-babel-tangle-block()
  (interactive)
  (let ((current-prefix-arg '(4)))
     (call-interactively 'org-babel-tangle)
))

(eval-after-load "org"
  '(progn
     (define-key org-mode-map (kbd "C-c b") 'org-babel-tangle-block)
))

and I can tangle the code block under the cursor with C-c b.

joon
  • 3,899
  • 1
  • 40
  • 53
  • Do you know if it is possible to print the name of the file that was just created instead of the .org file containing the source code block? – Guilherme Salomé Dec 18 '18 at 17:13
0

In extension to the helpful answer of joon I would like to share this snippet:

(defun org-babel-tangle-from-edit-special ()
    (interactive)
    (org-edit-src-exit)
    (let ((current-prefix-arg '(4)))
      (call-interactively 'org-babel-tangle))
    (org-edit-special))

...which can be combined with a keybinding like...

(add-hook 'org-src-mode-hook
  (lambda ()
   (local-set-key [f9] 'org-babel-tangle-from-edit-special)))

It allows you to tangle a source block from within the org-edit-special mode (so, whenever you hit C-c '). This is what I was actually looking for when finding this post.

Dennis Proksch
  • 240
  • 2
  • 9