[frameworks/ktexteditor] /: vi-mode: Fix block insert with tabs

Ismael Asensio <[email protected]>
Newsgroups gmane.comp.kde.cvs
Message-ID <[email protected]>
Git commit fc9cc218f103d71715c75a8917528edc071e7f50 by Ismael Asensio.
Committed on 27/07/2026 at 15:54.
Pushed by iasensio into branch 'master'.

vi-mode: Fix block insert with tabs

When inserting a text in visual block mode, the same text has to get
repeated at the same (visual) column on every line of the block.

Calculate the virtual column on each line to account for the visual
width of any possible tabs

BUG: 488801
FIXED-IN: 6.29

M  +4    -0    autotests/src/vimode/modes.cpp
M  +4    -0    src/vimode/modes/insertvimode.cpp

https://invent.kde.org/frameworks/ktexteditor/-/commit/fc9cc218f103d71715c75a8917528edc071e7f50

diff --git a/autotests/src/vimode/modes.cpp b/autotests/src/vimode/modes.cpp
index d8ccf44e3..7b4100207 100644
--- a/autotests/src/vimode/modes.cpp
+++ b/autotests/src/vimode/modes.cpp
@@ -1361,6 +1361,10 @@ void ModesTest::VisualCommandsTests()
     DoTest("foo\nbar\nbaz", "V>>V<<", "foo\nbar\nbaz");
     DoTest("    foo\n    bar\n    baz", "V2j<<", "  foo\n  bar\n  baz");
 
+    // Testing block prepend (BUG #488801 - also with tabs)
+    DoTest("foo\nbar\nbaz", "l\\ctrl-v2jIr\\esc", "froo\nbrar\nbraz");
+    DoTest("\t\tfoo\n        \tbar\n                baz", "wl\\ctrl-v2jIr\\esc", "\t\tfroo\n        \tbrar\n                braz");
+
     // Testing block append
     DoTest("averyverylongline\nshortline\nshorter\n", "jjV$kkAb\\esc", "averyverylonglineb\nshortlineb\nshorterb\n");
     DoTest("averyverylongline\nshortline\n", "V$jAb\\esc", "averyverylonglineb\nshortlineb\n");
diff --git a/src/vimode/modes/insertvimode.cpp b/src/vimode/modes/insertvimode.cpp
index ace5bdc8d..76e9c9568 100644
--- a/src/vimode/modes/insertvimode.cpp
+++ b/src/vimode/modes/insertvimode.cpp
@@ -489,6 +489,7 @@ void InsertViMode::leaveInsertMode(bool force)
             // make sure cursor haven't been moved
             if (m_blockRange.startLine == m_view->cursorPosition().line()) {
                 int start;
+                int vStart;
                 int len;
                 QString added;
                 KTextEditor::Cursor c;
@@ -506,8 +507,11 @@ void InsertViMode::leaveInsertMode(bool force)
                     added = getLine().mid(start, len);
 
                     c = KTextEditor::Cursor(m_blockRange.startLine, start);
+                    vStart = doc()->toVirtualColumn(c);
+
                     for (int i = m_blockRange.startLine + 1; i <= m_blockRange.endLine; i++) {
                         c.setLine(i);
+                        c.setColumn(doc()->fromVirtualColumn(i, vStart));
                         doc()->insertText(c, added);
                     }
                     break;
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.