diff --git a/src/plugins/texteditor/basetexteditor.cpp b/src/plugins/texteditor/basetexteditor.cpp
index db0fb8073514c27924e119a4556d18beef7969ce..8f51ed657452afe6313eff9ccab0c58888d5a015 100644
--- a/src/plugins/texteditor/basetexteditor.cpp
+++ b/src/plugins/texteditor/basetexteditor.cpp
@@ -928,8 +928,10 @@ void BaseTextEditor::keyPressEvent(QKeyEvent *e)
             cursor.insertBlock();
             indent(document(), cursor, QChar::Null);
         } else {
-            QString previousBlockText = cursor.block().text();
             cursor.insertBlock();
+
+            // After inserting the block, to avoid duplicating whitespace on the same line
+            const QString previousBlockText = cursor.block().previous().text();
             cursor.insertText(ts.indentationString(previousBlockText));
         }
         cursor.endEditBlock();