Published on

Using OrgMode Babel

Authors

Org-Mode Babel

I have spoken in the past about how I use OrgMode to take notes. I recently read an article about how an ex-developer quit his day job and decided to open a bakery. Being an ex-developer he setup emacs and PostgresSQL to help make his life easier in his new venture. This article is an interesting read in and of itself. One thing it introduced me to was the org-babel org-mode module.

This module allows you to specify code snippets in your org files and execute them. For example using a snippet from the official docs you would have your usual OrgMode content and a code block:

* Some Heading

#+BEGIN_SRC elisp
(* 7 2)
#+END_SRC

#+RESULTS:
: 14

In the above:

  • my code block is in-between #+BEGIN_SRC and #+END_SRC
    • The language in between the source in this case is elisp but can be changed to other supported languages
    • This block is executed by putting the cursor on any line in the code block and pushing: Ctrl+c Ctrl+c
  • #+RESULTS: has the result of executing the code block below it

When trying to use Org-babel you may run into various issues initially. I cover the issues I ran into and how I fixed them below.

Issue: Org-Mode Evaluation of code disabled

When you try run a code block for the first time you may get this issue. To fix it simply delete the org-plus-contrib package as per here:

rm -rf elpa/org-plus-contrib-*

Issue: No org-babel-execute Function for

To fix this you need to add a configuration for the language you want to be executable in your config file:

(org-babel-do-load-languages
  'org-babel-load-languages
  '((emacs-lisp . t)
    (python . t)
    (sh . t)))

In the above if you want to disable a language you use nil. For example emacs-lisp is enabled by default, to disable it change the above to: (emacs-lisp . nil)

See here for a full list of supported languages.

Issue: Older Packages

In my case the above was not enough to get babel to work. I suspected this may be due to my spacemacs installation having older packages. To update them and then recompile them in spacemacs do the following:

  • Update: M-x configuration-layer/update-packages
    • This should not take too long but can vary depending on how many packages there are to update.
  • Recompile: :spacemacs/recompile-elpa
    • This can take much longer as all your packages are being recompiled.

Conclusion

Org-babel is incredibly powerful as you can intermingle code and text together in the same document. I have only just started to use org-babel so am not fully aware of all its capabilities but it looks very promising.