org-mode source editing indents code after exiting source code block editor

1.2k Views Asked by At

In org-mode, for the given block:

#+BEGIN_SRC python
def hello():
    print('Hello world!')
#+END_SRC

I enter the source code editor with C-c ' and the syntax highlighting and indentation and everything else works correctly.

However, after I exit the mode with C-c ', 2 spaces of indentation are added so that the source code now appears like this:

#+BEGIN_SRC python
  def hello():
      print('Hello world!')
#+END_SRC

How do I fix it so that the indentation is preserved after editing the code block?

1

There are 1 best solutions below

0
On

You can preserve the indentation after editing a code block in org-mode by setting the following variables (thanks to Angelo Basile's article, "Org-mode Babel is cool" for the tip):

(setq org-edit-src-content-indentation 0
      org-src-tab-acts-natively t
      org-src-preserve-indentation t)