If I have a line of text in a file with leading whitespace, and I move to the beginning of that line and hit enter, the whitespace is deleted such that the first non-whitespace character is the first character of the line.

For example:

                Here is a line with leading whitespace.


Here is a line with leading whitespace.

if I hit enter at the start of the line (or actually anywhere within the whitespace).

Is there any way to stop Emacs from doing this? I would like to maintain the leading whitespace and just insert a new line.

  • 1
    Emacs only does this in modes where it is expected to make sense. E.g. vanilla emacs -Q foo.txt does not do this because foo.txt. will be in text-mode`. So you'll want to give more details about your example which can explain why you're seeing this.
    – Stefan
    Sep 3, 2020 at 16:26
  • I moved aside my ~/.emacs file to remove any customizations, and started emacs via emacs -Q foo.txt as you describe. I see that the file is in "Text" mode, but the leading whitespace is removed when I hit enter as I describe above. I'm running GNU Emacs 26.3 in FreeBSD 12.1. Not sure what other details you need - happy to provide them if there is anything in particular that would be helpful.
    – Jeff S
    Sep 3, 2020 at 16:33
  • [ The -Q makes Emacs ignore the .emacs anyway. ] Curious, because I tested this on Emacs-26.3 just before sending my comment. I did emacs -Q foo.txt where the file starts empty, then I did SPC a RET and the space before a remained and the cursor ended immediately under the a. You get a different result?
    – Stefan
    Sep 3, 2020 at 16:41
  • I did get a different result. You and I must have had different settings for electric-indent-mode (see below). Not sure why, but now that I know about this mode I can customize as I need. Thanks!
    – Jeff S
    Sep 3, 2020 at 16:49
  • 1
    The -Q forces the default setting (which enables electric-indent-mode by default for me as well). It's used specifically to factor out possible differences due to local configs.
    – Stefan
    Sep 3, 2020 at 17:01

1 Answer 1


C-h k RET (or M-x describe-key RET) displays:

RET (translated from ) runs the command newline [...]

Insert a newline, and move to left margin of the new line if it’s blank. [...]

If ‘electric-indent-mode’ is enabled, this indents the final new line that it adds, and reindents the preceding line. To just insert a newline, use M-x electric-indent-just-newline.

I guess you have electric-indent-mode activated.

So you can either use M-x electric-indent-just-newline (and possibly bind this to the RET key) or disable electric-indent-mode. You can add (electric-indent-mode -1) in your init file to disable it globally, or type M-x electric-indent-local-mode to disable it in the current buffer.

See the documentation for details (C-h f electric-indent-mode).

  • electric-indent-mode was exactly it... thanks so much! I didn't find it in my searches because I was looking for features related to "leading whitespace". It never occurred to me to search on "indent". Thanks again!
    – Jeff S
    Sep 3, 2020 at 16:41

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.