When I edit JavaScript code which exists in an HTML file, I lose code indentation whenever I press Enter.

first line of code:
    second line of code # Press Enter here

Result after pressing Enter:

first line of code
second line of code

(the indentation is lost)

How to prevent this?
P.S. I am using Emacs 25

  • In Markdown Mode, RET is already binding to markdown-enter-key by default. C-h c RET outputs RET (translated from <return>) runs the command markdown-enter-key from here.
    – xuchunyang
    Commented Aug 11, 2018 at 8:00

2 Answers 2


That's due to electric-indent-mode, which is a global minor mode.

You could add (electric-indent-mode -1) to your init file to disable it entirely, or you could try using a custom electric-indent-functions function.

The following is only minimally tested, but might do the trick:

(add-hook 'html-mode-hook 'my-html-mode-hook)

(defun my-html-mode-hook ()
  "Custom `html-mode' behaviours."
  (add-hook 'electric-indent-functions
            'my-html-mode-electric-indent-maybe nil :local))

(defun my-html-mode-electric-indent-maybe (_char)
  "Used in `electric-indent-functions'."
    (when (ignore-errors
            (string= (sgml-tag-name (sgml-parse-tag-backward))

It seems this bug is related to Emacs 25 because when I upgraded to version 25, I no longer am facing this problem.

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.