TL;DR: Using a persistent library of babel stored in one file can be a simple 3-step setup:
- Create an
org
-mode file ~/.emacs.d/library-of-babel.org
.
- Add a line
(org-babel-lob-ingest "~/.emacs.d/library-of-babel.org")
to your Emacs conf.
- Collect useful functions in that file, they will be read during emacs startup.
The Library-Of-Babel-file is where e.g. the aggregatebycol1
block from @mutbuerger would be saved to.
Another simple example use-case would be having a code-block, that generates
table data with a header row, but does not mark the headerrow with an 'hline
.
This is not tragic for simple display, but may make further automated processing more involved.
The solution here could be using a small code-block for post-processing from somewhere on the internet:
#+name: addhdr
#+begin_src emacs-lisp :var tbl=""
(cons (car tbl) (cons 'hline (cdr tbl)))
#+end_src
This will simply pipe through the data while splicing in an 'hline
as a second row.
To use this block later in other org files, simply add a :post
-processing stanza to your data-generating org source block:
#+NAME: Example
#+BEGIN_SRC elisp :post addhdr(*this*)
'(("Header1" "Column2" "Three")("R1C1V" "2" "C3R1")("4" "5" "6"))
#+END_SRC
#+RESULTS: Example
| Header1 | Column2 | Three |
|---------+---------+-------|
| R1C1V | 2 | C3R1 |
| 4 | 5 | 6 |
You can also easily give pre-existing tables to functions in your LOB:
#+NAME: ExData
| h1 | h2 |
| dh1r1 | dh2r1 |
| dh1r2 | dh2r2 |
#+CALL: addhdr(ExData)
In my library I have chapters to organize different types of functionality: Data-Generation, Filtering, PrettyPrinting, ...
Just remember to ingest
again after adding new blocks.
org-babel-library-of-babel
. When looking for source blocks, org-babel looks in the current file as well as the list stored in the above variable. The answer by @mutbuerger describes the details very well I think.