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. 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
    Feb 17, 2015 at 19:36
  • There are two replies already. lists.gnu.org/archive/html/emacs-orgmode/2015-02/msg00604.html
    – grettke
    Feb 18, 2015 at 2:27
  • Thanks, @grettke, for taking the initiative to post to the mailing list.
    – andreas-h
    Feb 19, 2015 at 11:30
  • 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 Jun 11, 2018 at 17:25

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
    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. Jun 11, 2018 at 17:22
  • This answer does not work with the new syntax for the structure templates.
    – T. Verron
    Apr 20, 2022 at 16:36
  • I'm out of the edit window, sorry. The template should now be something like ("py" . "src python :results output").
    – T. Verron
    Apr 20, 2022 at 16:47

Your Answer

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

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