diff --git a/src/plugins/texteditor/basetexteditor.cpp b/src/plugins/texteditor/basetexteditor.cpp
index 8f51ed657452afe6313eff9ccab0c58888d5a015..b9bcb9660dfdeb1fcbcfda5f640491f67fd8f57f 100644
--- a/src/plugins/texteditor/basetexteditor.cpp
+++ b/src/plugins/texteditor/basetexteditor.cpp
@@ -3243,11 +3243,10 @@ void BaseTextEditor::indentOrUnindent(bool doIndent)
     int pos = cursor.position();
     const TextEditor::TabSettings &tabSettings = d->m_document->tabSettings();
 
-
     QTextDocument *doc = document();
-    if (!cursor.hasSelection()
-        || (doc->findBlock(cursor.selectionStart()) == doc->findBlock(cursor.selectionEnd()) )) {
-        cursor.removeSelectedText();
+
+    if (!cursor.hasSelection() && doIndent) {
+        // Insert tab if there is no selection and indent is requested
         QTextBlock block = cursor.block();
         QString text = block.text();
         int indentPosition = (cursor.position() - block.position());;
@@ -3260,6 +3259,7 @@ void BaseTextEditor::indentOrUnindent(bool doIndent)
         cursor.removeSelectedText();
         cursor.insertText(tabSettings.indentationString(startColumn, targetColumn));
     } else {
+        // Indent or unindent the selected lines
         int anchor = cursor.anchor();
         int start = qMin(anchor, pos);
         int end = qMax(anchor, pos);