Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Org-mode how to call code block to evaluate from other org file?

Interesting in OrgMode version 9+

Is there a way to reference and evaluate code block from other org file and to use it's result in main code block in current org file?

Or maybe just to include code block from other org file to current code block to compose last one as

#+BEGIN_SRS lang :noweb yes
<<reusable-part1>>
<<current-buffer-specific-part>>
<<reusable-part2>>
#+END_SRS

and be possible to C-c C-c it.

like image 229
Dima Fomin Avatar asked Jan 03 '23 06:01

Dima Fomin


1 Answers

I think that Library of Babel is the best way to reuse code across org-mode files.

For any user to add code to the library, first save the code in regular ‘src’ code blocks of an Org file, and then load the Org file with org-babel-lob-ingest, which is bound to C-c C-v i

To be more explicit, lets say that you have a simple code like this one:

#+NAME: simpleShExample
#+BEGIN_SRC sh :var x="default X" :var y="default Y" :export none
  echo "Var: $x, $y"
#+END_SRC

This code can be reused with:

#+CALL: simpleShExample(y="My y")

which prints:

Var: default X, My y

Now if you save your code in an org file, for instance simpleShExample.org then you can reuse it in any other org files by loading it with this lisp command:

(org-babel-lob-ingest "path/to/simpleShExample.org")

Note that you can do that once in your emacs init file.

You can check the list of all the source code blocks you have ingested by inspecting the org-babel-library-of-babel variable.

like image 127
Picaud Vincent Avatar answered Mar 16 '23 00:03

Picaud Vincent