Indent code in org-babel src blocks

user103576 picture user103576 · Apr 2, 2013 · Viewed 9.2k times · Source

In an org-mode file, with code like the following:

#+begin_src emacs-lisp
(add-to-list 'org-tab-before-tab-emulation-hook
             (lambda ()
               (when (within-the-body-of-a-begin-src-block)
                 (indent-for-tab-command--as-if-in-lisp-mode))))
#+end_src

I would like the TAB key to indent the code as it would if it were in a buffer in lisp mode.

What I need is:

  • A way to figure out whether the cursor is within a src block. It needs to not trigger when on the header line itself, as in that case the default org folding should take place.
  • A way to indent the code according to the mode (emacs-lisp in this case) specified in the header.

Org can already syntax highlight src blocks according to mode, and the TAB hooks are there. This looks do-able.

Answer

TabFace picture TabFace · Dec 1, 2014

Since Emacs 24.1 you can now set the following option:

(setq org-src-tab-acts-natively t)

...and that should handle all src blocks.