What is the reason to strip whitespace when pressing Return?

In Make newlineAndIndent clear whitespace-only lines when terminating them · codemirror/commands@cf6bba4 · GitHub it looks like it was intentionally added to clear whitespace on whitespace-only lines when pressing Return.

A few of our users have requested that this doesn’t happen. The use case is adding blank lines in an indented block and then going back to add content to that line later. E.g. see the gif below. The middle steps are:

  1. Type bar
  2. Press Return twice
  3. Optional: Type baz
  4. Press ArrowUp

Expected: Indentation is maintained
Observed: Indentation has been removed and the user has to press Space a couple of times or Tab to start editing with the appropriate indentation.


We previously used Ace, which maintained the indent, as do Jetbrains editors. Sublime Text and VSCode behave the same as CM6, although it’s worth noting that there is a popular VSCode issue requesting the indentation is maintained: indent back to line start after enter newline and press up · Issue #40115 · microsoft/vscode · GitHub.

I also noticed a previous post on the CodeMirror issue tracker about this, but it was immediately closed by the OP Newline insertion removes spaces · Issue #1095 · codemirror/dev · GitHub.

So I’m just raising this as a vote for maintaining indentation, but am also curious if there is a good argument for keeping it as it is?

Just to make sure I understand, you are working in a context where blank lines with trailing space are an expected part of the code, and committed to the repository? Most linters I am familiar with complain about trailing space, and in general programmer culture, leaving that kind of spacing in is considered sloppy and undesirable. Since our default Enter binding adds auto-indentation, it also removes it again if the line is left blank, in order to not create this kind of mess.

You are free to bind Enter to another command of course. I could even be convinced to export a command that behaves this way (auto-indent, but don’t strip trailing space) to @codemirror/commands. But at the moment I don’t really see why it would be a good idea.

I work at Overleaf, so our users write in LaTeX and most of them would just be collaborating with other users and not commit to a repository as such. Some do collaborate with users from other IDEs though. There also aren’t very standardised rules for indenting in LaTeX, so we simply follow the indentation from the previous line when pressing Enter.

OK yes upon looking a bit more, it does seem to be more standard than I first believed to strip whitespace. It looks like some workflows get around this by stripping whitespace on save or on commit, but I can see why you wouldn’t want the spaces/tabs to remain in CM6 by default.

You are free to bind Enter to another command of course

Yes we can see how to change it for our use case if we want, although we’re trying to stick to the core CM6 behaviour where possible, to avoid extra maintenance work. I’ll discuss with the team, but we’ll probably stick as it is, thanks for clarifying.