Org-Mode Part 2 – execute code

When you want to learn and teach code, it can be useful to write the code in a document. This is also useful for the planning stage of a project. You may think that you just put comments in the code but that could be troublesome since comments need to show how this code works. In the document you can put project ideas, planning and abstract ideas that do not belong in the code commentary. The document can also contain results of the execution, which is powerful for drawing conclusions but has no place in final code.

Code in Emacs

Many developers use Emacs to develop software, it is a powerful code editor. In fact, many consider it an IDE, when correctly configured. But documents are usually separate from the code. If you want to demonstrate code or plan a new project you can use code snippets. To show how you believe it needs to work, you can write such a document. One powerful way to do this is to create an Org document. To add code to your document, you need to add a code block. A code block is a standard set of code which you can see below.

#+begin_src javascript
const {app, BrowserWindow} = require('electron')
const path = require('path')

The result can go to the document.

This example does not create an output, the simplest way to get output is to use shell. When you want to run a command and show the results, you need to add a header parameter.

#+begin_src shell :exports both
uname -a
uname -a

Linux mats-Ubuntu 5.0.0-20-generic #21-Ubuntu SMP Mon Jun 24 09:32:09
UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

The result is added by Emacs in block below the executing code. If you want more advanced results or you want to combine blocks , you will need to name your source code block. Add the optional #+NAME: tag. This tag works for tables as well as code.

Table 1.3.1:

x y z
0 1 4
1 3 6
2 4 8

With the calc module, you can put data in and have the results displayed in the document.

#+begin_src calc :var x=5 :var y=2 :exports both

First, the code is displayed, in the form you specify. Directly below, the results are shown.

25 a + 2

This is a very simple example, with support for LaTeX you can get any formula you want with the result in a very well formatted document. A


If you produce plots with your code, you can display that inside the document. Below, you can see a very simple code that uses gnuplot to plot the result of x2.

f(x) = x**2
plot f(x)

For this to work, you need to have gnuplot installed on your system. These features are all called from the system and presented in Emacs. The same goes for all programming languages that Emacs supports.

Not all code is supported, out of the box

For the sake of efficiency, not all supported languages are active in a vanilla install of Emacs. To make sure you have it active, you can add code to your emacs configuration, init.el is the standard file.

((python . t)
(emacs-lisp . t)
(shell . t)
(js . t)
(sqlite . t)
(calc . t))))

The above code enables six languages by setting them to ‘t’. the ‘t’ is the common way in Lisp to say true. You can also choose to set this value with the ‘M-x customize-variable’ method. When you do this, you need to type in the variable name, or group. In this case the variable is ‘org-babel-load-languages’. When you come to the page, you can see what is supported and activate the ones you want.

Adding a language

If your language is not in the list, you can look for language on https://orgmode.org/worg/org-contrib/babel/languages.Currently.html The list of supported languages is already long and is growing by the day. Note here, support for a language is only required for the evaluation. Viewing, exporting and tangling is supported without it. On the mentioned web page, you also have contributed languages that require a bit more work to install. If you still can’t see your language, there is a template for adding it yourself. This requires some lisp programming skills but if you look at the other languages you should be able to add it even with minor experience.


This article has only scratched the surface of what you can do with org-mode. More features are available and calling Emacs a simple editor is doing yourself a disservice.

About the author

Mats Tage Axelsson

I am a freelance writer for Linux magazines. I enjoy finding out what is possible under Linux and how we can all chip in to improve it. I also cover renewable energy and the new way the grid operates. You can find more of my writing on my blog.