Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/vim.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ export function initVim(CM) {
{ keys: "g$", type: "motion", motion: "moveToEndOfDisplayLine" },
{ keys: "g^", type: "motion", motion: "moveToStartOfDisplayLine" },
{ keys: "g0", type: "motion", motion: "moveToStartOfDisplayLine" },
{ keys: "g_", type: "motion", motion: "moveToLastNonWhiteSpaceCharacter", motionArgs: { inclusive: true }},
{ keys: '0', type: 'motion', motion: 'moveToStartOfLine' },
{ keys: '^', type: 'motion', motion: 'moveToFirstNonWhiteSpaceCharacter' },
{ keys: '+', type: 'motion', motion: 'moveByLines', motionArgs: { forward: true, toFirstChar:true }},
Expand Down Expand Up @@ -2537,6 +2538,12 @@ export function initVim(CM) {
return new Pos(cursor.line,
findFirstNonWhiteSpaceCharacter(cm.getLine(cursor.line)));
},
moveToLastNonWhiteSpaceCharacter: function(cm, head, motionArgs) {
var line = head.line + motionArgs.repeat - 1;
var lineText = cm.getLine(line);
var lastNonWS = findLastNonWhiteSpaceCharacter(lineText);
return new Pos(line, Math.max(0, lastNonWS));
Comment on lines +2543 to +2545
Copy link

Copilot AI Mar 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moveToLastNonWhiteSpaceCharacter computes line = head.line + motionArgs.repeat - 1 and immediately calls cm.getLine(line) without clipping the line number to the document bounds. With a large count (e.g. 100g_ near EOF) this can produce an out-of-range line and return a Pos with an invalid line, which then flows into operator selections (dg_, etc.) without being clipped and can break selection logic. Clamp the target line to [cm.firstLine(), cm.lastLine()] (or use cm.clipPos(new Pos(line, 0)).line) before calling getLine and before returning the Pos.

Suggested change
var lineText = cm.getLine(line);
var lastNonWS = findLastNonWhiteSpaceCharacter(lineText);
return new Pos(line, Math.max(0, lastNonWS));
var clippedLine = cm.clipPos(new Pos(line, 0)).line;
var lineText = cm.getLine(clippedLine);
var lastNonWS = findLastNonWhiteSpaceCharacter(lineText);
return new Pos(clippedLine, Math.max(0, lastNonWS));

Copilot uses AI. Check for mistakes.
},
moveToMatchedSymbol: function(cm, head) {
var cursor = head;
var line = cursor.line;
Expand Down Expand Up @@ -4018,6 +4025,15 @@ export function initVim(CM) {
return firstNonWS == -1 ? text.length : firstNonWS;
}

/** @arg {string} [text] */
function findLastNonWhiteSpaceCharacter(text) {
if (!text) {
return 0;
}
var index = text.search(/\s+$/);
return index == -1 ? text.length - 1 : index - 1;
}

/**
* @arg {CodeMirror} cm
* @arg {{inclusive?: boolean, innerWord?: boolean, bigWord?: boolean, noSymbol?: boolean, multiline?: boolean}} options
Expand Down
18 changes: 18 additions & 0 deletions test/vim_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,24 @@ testVim('g0_g$', function(cm, vim, helpers) {
is(!/\.$/.test(cm.getValue()));

},{ lineNumbers: false, lineWrapping:true, value: 'This line is long to test movement of g$ and g0 over wrapped lines.' });
testVim('g_', function(cm, vim, helpers) {
// Test basic g_ on line with trailing spaces
cm.setCursor(0, 0);
helpers.doKeys('g', '_');
helpers.assertCursorAt(0, 2);
// Test g_ on line without trailing spaces
cm.setCursor(1, 0);
helpers.doKeys('g', '_');
helpers.assertCursorAt(1, 2);
// Test g_ with count (2g_ goes to last non-whitespace of next line)
cm.setCursor(0, 0);
helpers.doKeys('2', 'g', '_');
helpers.assertCursorAt(1, 2);
// Test dg_ deletes to last non-whitespace (preserving trailing spaces)
cm.setCursor(0, 0);
helpers.doKeys('d', 'g', '_');
eq(' \nfoo', cm.getValue());
}, { value: 'foo \nfoo' });
Comment on lines +544 to +561
Copy link

Copilot AI Mar 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new g_ tests cover a small count (2g_) but don’t cover boundary behavior when the count would move past the end of the document. Since g_ is countable, adding a test like 100g_ on a short buffer (and asserting it lands on the last line’s last non-blank without throwing) would help lock in correct clipping behavior.

Copilot uses AI. Check for mistakes.
testVim('}', function(cm, vim, helpers) {
cm.setCursor(0, 0);
helpers.doKeys('}');
Expand Down
Loading