Org-mode source code blocks indent when I RET

When I click RET on any character other than the last in a line, in an Org-mode source code block, the whole block gets indented one tab. I then have to undo twice to get back to previous state. I cannot find anyone with similar problem online.

org-edit-src-content-indentation is a variable defined in ‘org-src.el’.

Its value is 2

Indentation for the content of a source code block.

This should be the number of spaces added to the indentation of the #+begin
line in order to compute the indentation of the block content after
editing it with ‘M-x org-edit-src-code’.

It has no effect if ‘org-src-preserve-indentation’ is non-nil.

I don’t mind the 2 spaces source code indentation. The problem is, whenever I RET my entire code block gets indented an extra 4 spaces.

The docstring of org-indent-line has the following:

  - In the code part of a source block, use language major mode
    to indent current line if ‘org-src-tab-acts-natively’ is
    non-nil.  If it is nil, do nothing.

I suggest you check the value of org-src-tab-acts-natively; it’s probably t for you. Then I think it’s the matter of the language major mode you use for the src block.

My guess is that it’s set to 4 spaces for the major mode, which gets added to org-edit-src-content-indentation as @akashp mentions above – my pure guess. I don’t use src block in Org mode extensively.

Yes, org-src-tab-acts-natively is t, which the documentation says it uses language major mode.

I get the message Can't guess python-indent-offset, using default: 4 when I use Python code blocks. python-indent-offset is set to 4. So yes it is set to 4 spaces.

I just counted, and it’s indenting 2 spaces every time I RET.

Just set org-edit-src-content-indentation to 0, and it’s stopped. Now my code blocks are not indented 2 spaces as they were before, which I think makes them less readable, so I’ll ask another question about that.