1

By default, Emacs quietly deletes trailing whitespaces once the user moves on to the next line. To reproduce the issue in plain Emacs, do this:

  1. Start plain Emacs: emacs --no-init-file --no-site-file
  2. Insert trailing tabs or spaces on the first line (e.g. five spaces).
  3. Press enter to go to the second line.
  4. Now go back to the first line. The spaces we inserted in step 2 have all disappeared!

This is not good. I want to preserve the trailing whitespace I have typed. Default Emacs seems to make it impossible for me to enter trailing whitespace.

Questions:

  1. What is the cause or rationale for this default behavior?
  2. How can I preserve the trailing whitespace I have entered?
  • I don't have this problem just writing a text file -- is this an issue with a specific file type (mode) that you're editing? If so, that will be worth mentioning in your question. – cyberbisson Aug 6 at 13:25
  • @cyberbisson The file I'm editing is named test.txt. Emacs uses text mode. I'm using Emacs 25.2.2. Older versions could have different behavior. What's your Emacs version? – Flux Aug 6 at 13:33
  • Oh sheesh, nevermind, I misread the problem. Sorry for the distraction -- I think JeanPierre has you on the right track. – cyberbisson Aug 6 at 14:26
2

Doing C-h k RET (describe-key RET) yields:

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

[...]

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.

So it seems to be a feature of indentation (command newline-and-indent does that also) that is here invoked automatically by elecric-indent-mode (which is active by default). You could avoid indentation by either:

  • disabling electric-indent-mode

  • binding RET to electric-indent-just-newline.

If you want to still be able to indent, without removing trailing whitespace, I suspect you can change the definition of newline-and-indent. Defined in simple.el, its code is:

(defun newline-and-indent ()
  "Insert a newline, then indent according to major mode.
Indentation is done using the value of `indent-line-function'.
In programming language modes, this is the same as TAB.
In some text modes, where TAB inserts a tab, this command indents to the
column specified by the function `current-left-margin'."
  (interactive "*")
  (delete-horizontal-space t)
  (newline nil t)
  (indent-according-to-mode))

Redefining without the delete-horizontal-space call seems to partially do the trick (but electric-mode seems to perform some more trailing whitespace removeal, I haven't studied this in detail).

  • Is there really no way to get automatic indentation using electric-indent-mode without destroying the trailing whitespace of the previous line? Binding RET to electric-indent-just-newline does not indent the new line. – Flux Aug 6 at 13:43
  • +1 for disabling electric-indent-mode. – Drew Aug 6 at 13:44
  • @Flux Possibly, see edit. – JeanPierre Aug 6 at 14:02
  • @Flux Possibly more complication, eg see emacs.stackexchange.com/questions/21868/… – JeanPierre Aug 6 at 14:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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