I have a weird behavior in org-mode. When I create a source block, pressing RET indents the current and all previous lines with 2 spaces, resulting in something like

#+begin_src bash :results output


No "manual" indentation was applied, it all came from pressing RET after each line.

I do not have this behavior if I press C-j instead instead of RET.

I also do not have it if I start Emacs with the -Q option, but I disabled all my org-mode configuration and had the same results, so I am not sure what is causing it.

  • I can reproduce that effect with emacs -Q and the built-in org-version Org mode version 9.4.3 (release_9.4.3 @ /usr/local/share/emacs/27.1.90/lisp/org/) . It is clearly a bug. You can set org-src-preserve-indentation to t as a workaround with the caveat that the content of source blocks is no longer indented.
    – Tobias
    May 18, 2021 at 3:38
  • Thanks! I'm running version 9.4.5 from https://orgmode.org/mepla/. I think that when I run emacs -Q I fall into the default org-mode from emacs 27.1, so the bug is not present. Hopefully someone has already filed a bug report. But for now your suggested workaround works great! Thanks again!
    – finotti
    May 18, 2021 at 9:36
  • Does this solve the problem for you: (electric-indent-mode -1)? Emacs decided to turn this mode on by default, which effectively swapped keys C-j and RET. Many of us turned this mode off as soon as Emacs turned it on by default.
    – Drew
    May 18, 2021 at 14:57
  • It does work, but not as well as Tobias's suggestion. It produces no indentation inside the code block (from the beginning of the line), which is good, but the previous way produces indentation to the code itself (like Python for loops, for instance). But thanks for the idea!
    – finotti
    May 18, 2021 at 18:43

1 Answer 1


This issue has been fixed in commit 857ae366b3 of the Org repo.

You can use that bugfix in Org version 9.4.3 (that comes with Emacs 27.1.90) by the following lines in your init files:

(with-eval-after-load "org"
  (when (version-list-= (version-to-list org-version) '(9 4 3))
    (defun org-return-fix (fun &rest args)
      "Fix https://emacs.stackexchange.com/questions/64886."
      (let* ((context (if org-return-follows-link (org-element-context)
             (element-type (org-element-type context)))
    (if (eq element-type 'src-block)
        (apply #'org--newline args)
      (apply fun args))))
    (advice-add 'org-return :around #'org-return-fix)))

(with-eval-after-load "org-src"
  (when (version-list-= (version-to-list org-version) '(9 4 3))
    (defun org-src--contents-for-write-back ()
      "Return buffer contents in a format appropriate for write back.
Assume point is in the corresponding edit buffer."
      (let ((indentation-offset
         (if org-src--preserve-indentation 0
           (+ (or org-src--block-indentation 0)
          (if (memq org-src--source-type '(example-block src-block))
        (use-tabs? (and (> org-src--tab-width 0) t))
        (source-tab-width org-src--tab-width)
        (contents (org-with-wide-buffer (buffer-string)))
        (write-back org-src--allow-write-back))
      ;; Reproduce indentation parameters from source buffer.
      (setq indent-tabs-mode use-tabs?)
      (when (> source-tab-width 0) (setq tab-width source-tab-width))
      ;; Apply WRITE-BACK function on edit buffer contents.
      (insert (org-no-properties contents))
      (goto-char (point-min))
      (when (functionp write-back) (save-excursion (funcall write-back)))
      ;; Add INDENTATION-OFFSET to every non-empty line in buffer,
      ;; unless indentation is meant to be preserved.
      (when (> indentation-offset 0)
        (while (not (eobp))
          (skip-chars-forward " \t")
          ;; (unless (eolp)     ;ignore blank lines
          (let ((i (current-column)))
        (delete-region (line-beginning-position) (point))
        (indent-to (+ i indentation-offset)))
  • 2
    This works perfectly! I'm using version 9.4.5, so I had to adjust the (version-to-list org-version), but now it just works! I greatly appreciate the help!
    – finotti
    May 20, 2021 at 11:44
  • @finotti If this answers your question just click the V-button below the score. This marks the question as done on the overview page. Thx.
    – Tobias
    May 20, 2021 at 16:55

Your Answer

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

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