1

So, I need to pass the content of one Org source block into another.

There are a couple of ways. One can use :noweb header and reference content in the second block using <<first-block-content>>. Or you can use :var header, but then you have to keep the first block as #+begin_example, not #+begin_src.

My problem is a bit different. I want to refer to the content of the first block as a filename. E.g.,

#+name: block-1
#+begin_src json :tangle "some-file.json"
{"example": "example json"}
#+end_src

#+begin_src sh 
cat some-file.json | jq
#+end_src

And this kinda works, right? Except, there's one caveat that's bugging me. You still have to manually tangle the file from the first block. And I want to find a way to avoid that. Whenever I eval the second block, I want it to automatically find the first block, tangle its file, and only after that eval the second block. And I just can't figure this out.

1 Answer 1

3

You can achieve this by using :results file in the first block, and then by using the result of block-1 in your second block via `:var in=block-1 (where you simply discard the result), you can trigger execution of block-1 'from' your second block, e.g:

#+name: block-1
#+begin_src emacs-lisp :results file silent :file some-file.el
'(:example "example json")
#+end_src

#+begin_src sh :var input=block-1
cat some-file.el
#+end_src
3
  • 2
    You can do a bit better: don't silence the results from the first block and use $input in the shell command in the second block. That loosens the tight coupling between the two blocks a bit.
    – NickD
    May 6, 2023 at 16:31
  • 1
    That's a nice comment indeed. I noticed that it is not necessary to remove the silent from the first code block. May 6, 2023 at 16:58
  • 1
    Ah right:silent is only for the benefit/detriment of human eyes :-) As a minot point, note that if you execute the second block, it runs the first block but it does not insert the output in the buffer (silent or not).
    – NickD
    May 6, 2023 at 17:14

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.