Tangling Examples in Emacs

Tried literate programming with Org and Emacs, but want to know the details of what files goes where and how to specify it? Let’s look at each approach with some examples.

First, the default value for the tangle property is no, but we could change this per file (not globally that I’ve found), with the following setting somewhere in your org file:

PROPERTY: header-args :tangle yes

However, to get this to actually work, you need to enter C-c C-c on that line. If you read the file into Emacs, those lines are automatically operated, so the C-c C-c trick is only needed when added or changed.

Emacs bases the filename for the source code on the original org file. For instance, here is example-1.org:

#+PROPERTY:    header-args :tangle yes

Just an example.

#+begin_src sh
echo "Hello"
#+end_src

And another language:

#+begin_src python
print("Hello")
#+end_src

Creates two files, example-1.sh:

echo "Hello"

And example-1.python:

print("Hello")

Wait? By example-1.python? Shouldn’t the extension be .py? Well, it should. Normally, if I don’t use the yes parameter, and instead specify what filenames (and its location) I want to store all of those files. Let’s try example-2.org that has more control:

#+PROPERTY:    header-args:shell :tangle no
#+PROPERTY:    header-args:python  :tangle /tmp/tangling-examples/example-2.py

Just an example.

#+begin_src shell
echo "Hello"
#+end_src

And another language:

#+begin_src python
print("Hello")
#+end_src

Hitting C-c C-c on both of those two lines sets the configuration so that when we tangle, C-c C-v t, we get a single file, example-2.py with the correct extension.

Tangled Results per Section

The previous example shows my typical use case, however, you can get create