Emacs: Org Mode, Work with Source Code (org-babel)

By Xah Lee. Date: . Last updated: .

What is Org-Babel

org-mode can embed programing language source code, and you can interactively evaluate, edit, or export to HTML with evaluated results embedded/updated in the exported file.

(This feature is known as org-babel.)

emacs org mode babel 2020-08-16 Dtq32
emacs org mode babel 2020-08-16

Source Code Marked Up

Source code in org mode is marked up like this:

Example of python code:

#+BEGIN_SRC python
print 3
#+END_SRC

Example of emacs lisp code:

#+BEGIN_SRC emacs-lisp
(+ 3 2)
#+END_SRC

Key Shortcut

Alt+x org-insert-structure-templateCtrl+c Ctrl+,

insert a code block markup. (new in Emacs 27 (Released 2020-08))

Keyword of Programing Languages

Here's the a short list of keywords for popular programing languages. For a complete list, see https://orgmode.org/worg/org-contrib/babel/languages/index.html

• C • C++ • D • R • awk • clojure • css • emacs-lisp • eshell • fortran • gnuplot • haskell • java • js • latex • lisp • lua • matlab • ocaml • octave • org • perl • processing • python • ruby • sass • scheme • sed • sh • sql • sqlite • vala

Evaluate Code

To evaluate code block, put cursor in the code block, then press Ctrl+c Ctrl+c. (it calls org-ctrl-c-ctrl-c)

Here's a example you can copy paste into org mode file to test.

#+BEGIN_SRC emacs-lisp
(+ 3 4)
#+END_SRC

#+RESULTS:
: 7

Setup Which Languages to Allow Eval

By default, only emacs lisp code is allowed to eval. To allow others, put this in your Emacs Init File:

(require 'org)
(require 'ob)

;; make org mode allow eval of some langs
(org-babel-do-load-languages
 'org-babel-load-languages
 '((emacs-lisp . t)
   (python . t)
   (ruby . t)))

Here's a example of Python. Try to eval it.

#+BEGIN_SRC python
return 3 + 7
#+END_SRC

#+RESULTS:
: 10

Note: for Python, when in non-session mode, you need to have a return statement at the end, in order to print result, else the result is None.

Stop Emacs from Confirming Eval

Everytime you eval a source code in org mode, emacs will ask you to confirm.

To stop asking, put this in your Emacs Init File:

;; stop org-mode asking for confirmation when eval code
(setq org-confirm-babel-evaluate nil)

Or, if you want it to be per file, you can put the following at the top of your org file, and eval it everytime you open that file.

#+BEGIN_SRC emacs-lisp
;; stop org-mode asking for confirmation when eval code
(setq-local org-confirm-babel-evaluate nil)
#+END_SRC

Turn on Syntax Coloring

To make org mode syntax color embeded source code, put this in your Emacs Init File:

(setq org-src-fontify-natively t)

Editing Source Code

• When cursor is inside the source code block, Alt+x org-edit-specialCtrl+c '】 to create a new buffer in split pane, dedicated to the language.

• edit it, and Alt+x org-edit-src-saveCtrl+x Ctrl+s】 will save it back in the original org mode file.

Alt+x org-edit-specialCtrl+c '】 again to remove this edit mode buffer.

Org Babel History

Org Babel is written by Eric Schulte. [https://eschulte.github.io/] Thank you Eric Schulte.

Org Mode