emacsorg-modeorg-babel

How can I have global :var binding for org-mode code block header?


With emacs/org-mode, I can define variable binding per code block, as demonstrated below.

#+HEADER: :var release="release-0.5"
#+begin_src shell :results output verbatim
echo $release
#+end_src

#+RESULTS:
: release-0.5

#+HEADER: :var release="release-0.5"
#+begin_src shell
echo "Indeed the release is $release"
#+end_src

#+RESULTS:
: Indeed the release is release-0.5

However, I wonder if there is a way to avoid the duplication, to define the variable binding globally, or per section?

I tried the following, it does not work:

* Release Package the script to be portable
:PROPERTIES:
:header-args:var: release="release-0.5"
:END
#+begin_src shell :results output verbatim
echo $release
#+end_src

#+RESULTS:
: 
#+begin_src shell
echo "Indeed the release is $release"
#+end_src

#+RESULTS:
: Indeed the release is

EDIT: (adopting @Enze Chi's suggestion) The following still doesn't work:

:PROPERTIES:
:header-args:shell:var release="release-0.5"
:END
#+begin_src shell :results output verbatim
echo $release
#+end_src

#+RESULTS:
:

#+begin_src shell
echo "Indeed the release is $release"
#+end_src

#+RESULTS:
: Indeed the release is

I wonder if there is a way to make it work?


Solution

  • The name of the property is header-args:shell; the value of the property is :var release="release-0.5". The syntax of a properties drawer is 1:

    :PROPERTIES:
    :NAME: VALUE
    :END:
    

    so in this case:

    :PROPERTIES:
    :header-args:shell: :var release="release-0.5"
    :END:
    

    Note that the name of the property has to be wrapped in colons (:) and there has to be a space after the ending colon to separate the name part from the value part, because that's how property names are syntactically recognized.

    So the colon before and after the name of the property (header-args:shell) is part of the property drawer syntax, the colon in header-args:shell is part of the name itself, and the colon in :var release="release-0.5" is part of the value itself; and you need a space between the "colonized" name and the value. You got to keep your colons straight!

    See Property Syntax and Using header arguments in the Org mode manual for all the details.


    [1] Note that the property drawer must follow the heading without any empty lines. If the heading has planning information (SCHEDULED or DEADLINE), the property drawer follows the planning information, again without any empty lines (either between the heading and the planning information or between the planning information and the property drawer). See Property syntax in the manual or Property drawers in the Org syntax document on Worg.