0

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 Aug 11 '18 at 8:00
1

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'."
  (save-excursion
    (when (ignore-errors
            (string= (sgml-tag-name (sgml-parse-tag-backward))
                     "script"))
      'no-indent)))
0

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 acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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