Org-Mode Part 2 – execute code

Chưa phân loại
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 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.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

How to Install STEAM to Play Games on Ubuntu 18.04 LTS

STEAM is a gaming platform developed by Valve Corporation. STEAM hosts many games on their servers, which can be accessed...
Chưa phân loại, vps giá rẻ

Hướng dẫn cài đặt FFMPEG trên CentOS 7/8

FFMPEG là một công cụ hỗ trợ chuyển đổi định dạng các file audio, và video trên Linux. Nó cho phép...

How to Delete Directory in Linux

You can delete directories from the command line in Linux very easily. If you have any graphical desktop environment installed...
Chưa phân loại, vps giá rẻ