I would like to add snippets of code for several languages to my org files.

The code I enter has syntax highlighting, but does use correct indetation. One language I use is python, and I have seen here, that what I want is possible. In the linked video, referring to entering code snippets with the correct syntax highlighting and so fourth, Matthew says:

"... provided you have configured the appropriate settings...".

I am not sure what I could be missing as, for example, my python works just fine in a python buffer.

I always need to open a separate buffer to achieve this, as mentioned here. The OP in that answer pasted his code in from somewhere else, but I am writing mine on the fly. Also, I do not really wish to use the accepted answer there, which is the timed-running of a custom function

As a clear example, I enter the code here, leaving my cursor at the underscore, _ :

#+begin_src python
for i in range(1:23): _
∫   √


and when I press enter, I end up at the integral sign, . I would of coursel ike to end up at the square root symbol (sorry for overkill on symbols!). That means 4-space indent.

My Emacs 24.5 (9.0) on Mac OSX automatically inserts the #+begin_src and #+end_src all in upper case. I have tried both out and it doesn't make a difference.

I know there are some parameters one can set in the opening line, after having written the mode, in my case python is there. Is there a parameter to specify something like indentation in the code snippet?

  • Setting (setq org-src-tab-acts-natively t) in your init file should allow you to use the tab key to indent correctly with a single keystroke instead of pressing spacebar four times (which it sounds like is your current situation), but I have not yet found a way to get automatic/electric indentation in org source blocks.
    – elethan
    Commented Dec 17, 2015 at 19:18
  • 1
    What the guy in the video does as far as I can tell (and what I do) is use org-edit-special (C-c ') to open the source block in a buffer in the language's mode where everything should work as expected, and then use C-c' again to go back to your org buffer.
    – elethan
    Commented Dec 17, 2015 at 19:27
  • I was hoping there might be a better, more convenient way. That video is now more that 2 years old. There are options (variables) to set in your unit file to deal with indentation in src snippets, but they only seem to alter how the snippets are exported. For example: (setq org-edit-src-content-indentation 4) - meaning 4 spaces before the #+begin_src in e.g. AucTex output.
    – n1k31t4
    Commented Dec 17, 2015 at 19:39


Your Answer

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

Browse other questions tagged or ask your own question.