I want to understand how org-babel's Library Of Babel works. Seems like a powerful yet underused tool.

The documentation says that I can

add code to the library, by first saving 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.

What is that org-babel-lob-ingest truly doing? It is just appending all source blocks within an Org File to another file?

And what are the use cases of this? Can I see what I have in my Library interactively? Can I use noweb syntax with source blocks within the Library? What do I need to do to start using it?

Any examples and links to tutorials are very welcome.

  • It is appending the list of named source blocks it finds in the file to the variable 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. – NickD Apr 4 '18 at 18:30

There is a nice introduction to the library of babel in library-of-babel.org which is located in Org’s source directory. To use those examples of named source-code blocks in other files, populate the org-babel-library-of-babel variable with

#+begin_src elisp :results scalar
(org-babel-lob-ingest "/path/to/org-mode/doc/library-of-babel.org")

: 21

One of the 21 blocks is named “transpose” and probably does what it’s supposed to do:

#+name: tbl
| a | 1 |
| d | 2 |
| a | 3 |
| d | 4 |
| d | 5 |
| c | 6 |

#+begin_src elisp :results table :post transpose(table=*this*) :var var=tbl

| a | d | a | d | d | c |
| 1 | 2 | 3 | 4 | 5 | 6 |

You may as well add your own code blocks, especially something you may find yourself using a lot in the future. As an example, the following allows me to aggregate values in the named table by the first column:

#+name: aggregatebycol1
#+begin_src elisp :results table :var table='() fun='()
(let (res)
   (lambda (x)
     (push `(,(car x) ,(apply fun (mapcar 'cadr (cdr x)))) res))
   (seq-group-by 'car table))
  (nreverse res))

Save the block in any file and add it to org-babel-library-of-babel:

#+begin_src elisp :results scalar
(org-babel-lob-ingest (buffer-file-name))

: 1

#+header: :post aggregatebycol1(table=*this*, fun='+)
#+begin_src elisp :results table :var var=tbl

| a |  4 |
| d | 11 |
| c |  6 |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.