Putting link references to lines of code in a source block

| categories: org-mode | tags:

I keep forgetting about this interesting gem of a feature in org-mode code blocks. You can put references to specific lines of code outside the block! http://orgmode.org/manual/Literal-examples.html#Literal-examples

The following code block has some references in it that we can refer to later:

#+BEGIN_SRC emacs-lisp -n -r
(save-excursion (sc)
  (goto-char (point-min))) (jump)
1: (save-excursion
2:   (goto-char (point-min)))

In line

we remember the current position.
jumps to point-min.

To make this work with python we have to make a slight change to the reference format in the header.

#+BEGIN_SRC python -n -r -l "#(ref:%s)"
for i in range(5):                # (for)
    print i                       # (body)
1: for i in range(5):
2:     print i

In line

we initialize the loop, and in line
we run it.

Copyright (C) 2014 by John Kitchin. See the License for information about copying.

org-mode source

Org-mode version = 8.2.5h

Discuss on Twitter