How to org-babel-tangle only one code block?
Asked Answered
W

3

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?)

Wear answered 25/2, 2015 at 18:48 Comment(0)
T
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.

Tar answered 25/2, 2015 at 20:37 Comment(0)
W
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.

Wear answered 22/9, 2016 at 1:35 Comment(1)
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?Shoulder
U
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.

Uncap answered 28/8, 2021 at 8:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.