1.
First thing that comes to mind is :session <session_name>
code block that share the same session name form part of the same namespace.
You will need to open all the org files with concerning code and execute it to using the :session header argument (empty loads all code into default session or supply a session name).
#+src_begin sh :session my_session_01
2.
Use :tangle
to write all your code (of the same language) to a file. for example:
#+begin_src python :tangle my_code.py
All the code in an org file can be written with one command, C-c C-v f
M-x org-babel-tangle-file
Example in python
First code block:
#+BEGIN_SRC python :results output :tangle ~/tangled.py
a = 'ape'
b = 'bat'
#+END_SRC
Second code block:
#+BEGIN_SRC python :results output :tangle ~/tangled.py
def tangled(x, y):
return x + y
print(tangled(a, b))
#+END_SRC
running python tangled.py
in bash gives:
[adam@adam-pc ~]$ python tangled.py
apebat
Which is what's expected from the two pieces joint.
Loading the tangled file to a new src code in another org file will be dependent on language. I used a general cumbersome method in this case of Python, I normally will have added an __init__.py
file in that folder and that would have made importing much more streamline, I rather have in this case a simple to read code, since it's not about the specifics of any language therefore this is the code that includes the import plus the result:
#+BEGIN_SRC python :results output org drawer
import sys
import os
sys.path.append("/home/adam/")
from tangled import *
print(tangled('eggs',' and spam')
#+END_SRC
:RESULTS:
>>> eggs and spam
:END:
I am sure this can be improved with a header argument. perhaps in :properties:
I will look at that and edit accordingly.
header arguments in org-babel
the org manual - headers
Edit 1:
In case the org file code blocks are of the same language you can tangle globally using the following property to influence the entire buffer:
#+PROPERTY: header-args :tangle yes