19

Is there a way to define a default language for org-mode code blocks in the docuement header? Usually, within one org document, I only use one programming language, so it's getting tiresome to always write

#+BEGIN_SRC python
[...]
#+END_SRC

It would be nice to be able to do something like

#+PROPERTY: header-args :session *mysession* :tangle yes :results silent :language python

[...]

#+BEGIN_SRC
[...]
#+END_SRC

Is there any way to achieve this?

6
  • 2
    This does not answer the question on how to set the default language, but it will definitely save you a lot of typing. Typing <s followed by the TAB key will auto expand into the source code block structure and then you simply need to type python, go to the next line and fill in the code. Commented Feb 17, 2015 at 13:46
  • Check out orgmode.org/manual/… . Seems like the language spec is required. Did you post on the org list? If you didn't, I will, this is an interesting and probably common desire.
    – grettke
    Commented Feb 17, 2015 at 19:36
  • 1
    There are two replies already. lists.gnu.org/archive/html/emacs-orgmode/2015-02/msg00604.html
    – grettke
    Commented Feb 18, 2015 at 2:27
  • 1
    The hint from the mailing list above (if one is looking for a solution that does not involve having #+BEGIN_SRC python in the .org file) is Probably you would have to modify org-babel-get-src-block-info so that it returns a default when there is nothing after #+BEGIN_SRC Commented Jun 11, 2018 at 17:25
  • 1
    Specifically, in ob-core.el, the function org-babel-get-src-block-info uses org-element-property to get the language: code looks like: (let* ((lang (org-element-property :language datum)).... This org-element-property is defined in og-element.el, and it should be possible to advise or replace this org-element-property, to return a default when property is :language. Commented Jun 11, 2018 at 17:38

1 Answer 1

12

This answer provides a workable workaround.

The easiest way to do this would be using easy templates. Add your templates to the org-structure-template-alist:

(add-to-list 'org-structure-template-alist
         '("py" "#+BEGIN_SRC python :results output\n?\n#+END_SRC" ""))

The question mark indicates the cursor position. Insert the code block with <py TAB in this case.

4
  • 2
    Your answer is how to add new templates for expansion, not to default the language for a source block.
    – grettke
    Commented Feb 17, 2015 at 19:35
  • While this solves the problem of making it easy to enter such blocks, it doesn't solve the "problem" that the source file still contains #+BEGIN_SRC python instead of just #+BEGIN_SRC -- this makes the source file a bit harder to read, for example. Commented Jun 11, 2018 at 17:22
  • This answer does not work with the new syntax for the structure templates.
    – T. Verron
    Commented Apr 20, 2022 at 16:36
  • 1
    I'm out of the edit window, sorry. The template should now be something like ("py" . "src python :results output").
    – T. Verron
    Commented Apr 20, 2022 at 16:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.