Skip to content

Fix Vim cw to change through word end - #173

Merged
ernestrc merged 5 commits into
unstablebuild:mainfrom
santhiprakash:fix/152-vim-cw-end-of-word
Oct 1, 2026
Merged

ernestrc merged 5 commits into
unstablebuild:mainfrom
santhiprakash:fix/152-vim-cw-end-of-word

Conversation

@santhiprakash

Copy link
Copy Markdown
Contributor

Fixes #152.

In Vim, cw is a ce in disguise: the change stops at the end of the current
word and keeps the whitespace after it, while dw consumes that whitespace.
Rune routed cw through the same start-word motion as dw, so changing a word
in the middle of a line also ate the separator before the next word.

handleDelete now selects the word-end range for cw/cW before falling
through to the generic motion path. A word end already under the cursor counts
as the first end, so cw at a word end changes just that cell instead of
reaching into the following word, and on a blank the plain w motion applies,
matching Vim. dw, yw, text objects, and the other operators are unchanged.

Verified against Vim 9.1 for start/middle/end of word, single-letter words,
counts (2cw, c2w, 2c2w), cW, and blank/empty-line cases. A few existing
count tests encoded the dw-style behavior and are updated to the Vim
expectations (e.g. 2cw on one two three four now yields three four,
keeping the separator). go test -race ./internal/text/... passes.


Authorship

  • Written by hand. I would like a human review.
  • Mostly or partially generated by an AI agent, which I reviewed and understand.

In Vim `cw` is a `ce` in disguise: the change stops at the end of the
current word and keeps the whitespace after it, while `dw` consumes that
whitespace. Routing `cw` through the same start-word motion as `dw`
deleted the separator users intended to keep. Select the word-end range
for `cw`/`cW` instead, counting a word end already under the cursor as
the first end so `cw` at a word end still changes just that cell. On a
blank the plain `w` motion applies, matching Vim.

Signed-off-by: Santhi Prakash <[email protected]>
@santhiprakash santhiprakash mentioned this pull request Oct 1, 2026
2 tasks done
@santhiprakash

Copy link
Copy Markdown
Contributor Author

Note on CI: the ubuntu-24.04 leg fails on TestRenderPreset (cmd/rune) — it fails identically on current main (5d093621f, run 36801055734 vs main run 36786513529), so it is inherited upstream breakage rather than something from this change. All other checks pass, including golangci-lint and the macos-15 test leg.

@codecov-commenter

codecov-commenter commented Oct 1, 2026 •

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 62.74510% with 19 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
internal/text/cursor.go 29.62% 19 Missing ⚠️

📢 Thoughts on this report? Let us know!

Comment thread internal/text/vi/handler.go Outdated
if bigWord {
return false
}
return !isWordRune(cur) || !isWordRune(next.Ch)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

atWordEnd uses different word rules from MoveRightEndWord.

This uses Vim's word classes (letters, digits, _), but Cursor.MoveRightEndWord (internal/text/cursor.go:1038) breaks words on allSpecialCharacters (cursor.go:971). There, _ is a separator, while $, ~ and non-ASCII punctuation are not. When the two disagree, count gets decremented wrongly and the change overshoots:

buffer keys this PR Vim
a$b x c2w x b x
ab$ x c2w `` (whole line) x

Suggest exposing an "at word end" check from text.Cursor that uses the same character table as e, rather than keeping a second set of rules in vi.

Comment thread internal/text/vi/handler.go Outdated
return !isWordRune(cur) || !isWordRune(next.Ch)
}

func isWordRune(r rune) bool {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

isWordRune / isWordBlank duplicate isWordObjectRune / isWordObjectBlank in internal/text/cursor.go:1094-1100. If the check moves into text.Cursor (see the comment on atWordEnd), these can go away.

Comment on lines +3304 to +3309
for i := 0; i < count; i++ {
prev := vi.cursor.CursorAtScroll()
if !move() && vi.cursor.CursorAtScroll() == prev {
break
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Regression: counted cw across an empty line leaves part of the next word.

Routing cw through MoveRightEndWord picks up an existing bug in how e crosses empty lines (d2e is wrong on main too), and the result is worse than before:

buffer keys main this PR Vim
one\n\ntwo wc2w \ntwo wo
one\n\ntwo c2w \ntwo wo ``

Either fix how e crosses empty lines first, or fall back to the previous w path when the motion would cross a line.

Comment thread internal/text/vi/handler.go Outdated
Comment on lines +3277 to +3283
// selectChangeWord selects the range a `cw`/`cW` delete acts on and reports
// whether the event is such a motion. In Vim `cw` is a `ce` in disguise: the
// change stops at the end of the word instead of consuming the whitespace
// after it, and a word end already under the cursor counts as the first end
// reached, so a single `cw` there covers just the cell under it where `ce`
// would reach the following word's end. On a blank the plain `w` motion
// applies.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: this comment mostly explains how the function works. Docstring comments should state the contract: what range gets selected, the return value, and the edge cases (i.e. word end under the cursor, blank under the cursor), not narrate the code. The code should narrate itself.

Comment thread internal/text/vi/handler_test.go Outdated
{name: "d2w from line end over empty line joins lines", content: "one\n\ntwo", seq: "$d2w", wantContent: "on\ntwo", wantScroll: coord(1, 0)},
{name: "c2w over two empty lines keeps one line", content: "\n\nthree", seq: "c2wX", wantContent: "X\nthree", wantMode: insertMode, wantScroll: coord(1, 0)},
{name: "c2w from indented word over empty line keeps indent", content: " one\n\ntwo", seq: "wc2wX", wantContent: " X\ntwo", wantMode: insertMode, wantScroll: coord(3, 0)},
{name: "c2w from indented word over empty line keeps indent", content: " one\n\ntwo", seq: "wc2wX", wantContent: " Xwo", wantMode: insertMode, wantScroll: coord(3, 0)},

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This expectation was changed to match the bug in selectChangeWord (see the comment on the move loop). " Xwo" matches neither the old behaviour (" X\ntwo") nor Vim (" X"), so the test now locks in the bug. It should expect Vim's result and fail until the empty-line case is fixed.

}
}

// cw changes to the end of the word like ce, keeping the whitespace after

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please significantly expand the set of test cases here (i.e. where the two word rules disagree, plus a counted cw across an empty line, wide chars, nulls, oob, etc.):

  • a$b x + c2w → b x
  • foo_bar x + cw → x (currently _bar x, because e treats _ as a separator; this already happens on main)
  • one\n\ntwo + c2w → ``

The first and last would have caught the two bugs above.

Review feedback showed the previous change kept a second word-rule set
in the vi handler that disagreed with the e-motion table: counted cw
overshot on punctuation and underscores (a$b x + c2w, foo_bar x + cw),
and crossing an empty line left part of the next word behind. The
word-end check and motion now live on text.Cursor as AtWordObjectEnd
and MoveRightWordObjectEnd, sharing the wordClass table used by the
aw/iw text objects. The empty-line expectation is corrected to Vim's
result and the matrix now covers punctuation runs, underscores, wide
runes, null cells, empty-line crossing, and out-of-bounds cursors.

Signed-off-by: Santhi Prakash <[email protected]>
@santhiprakash

Copy link
Copy Markdown
Contributor Author

Thanks for the detailed review — all six points addressed in 40a2f6dd.

Root cause. The word-end check lived in vi with its own rule set (letter/digit/_), while the end-motion it counted with (MoveRightEndWord) splits on allSpecialCharacters — _ is a separator there and $/~/non-ASCII punctuation are not. Where the tables disagreed, the count decremented wrong and the change overshot, exactly as your cases show.

Rework. The check and the motion moved into text.Cursor: AtWordObjectEnd(bigWord) and MoveRightWordObjectEnd(bigWord), both defined on the existing wordClass table the aw/iw text objects already use (IsWordObjectBlank/IsWordObjectRune are exported for this). The vi-side atWordEnd/isWordRune/isWordBlank helpers are deleted — one rule set now.

Empty-line crossing. Fixed as a property of the new motion rather than by touching e: MoveRightWordObjectEnd never treats a blank run or an empty line as a word end, so one\n\ntwo + c2w lands on the o of two and produces Vim's empty result, and wc2w on one\n\ntwo gives . I did not change e/d2e's empty-line behavior on main since that's a pre-existing separately-scoped bug — happy to take it in a follow-up if you want it.

Tests. " Xwo" corrected to Vim's " X". The matrix now covers all your rows plus punctuation runs (a++b x), cW on a-b c, tabs, a wide rune (a字b x), a null cell (a\x00b x), c9w past buffer end, and out-of-bounds cursors (past-last-column clamps and changes the last cell; past-last-line is a no-op). Docstrings now state the contract rather than narrating the code.

Verification.

  • go test ./internal/text/... — all packages pass
  • go test -run 'TestViChangeWord|TestViCountedOperatorScenarios|TestViWordMotionEmptyLines' ./internal/text/vi/ — green
  • go build ./... — clean; gofmt — clean
  • Sabotage: restoring the previous implementation fails exactly the reviewed cases (a$b x+c2w, ab$ x+c2w, foo_bar x+cw, punctuation runs, both empty-line cases, underscore)
  • Real Vim 9.1 driven in tmux confirms every new expectation except the wide-rune case, where Vim's utf_class granularity differs — rune treating 字 as a word rune is self-consistent, flagging it rather than claiming parity. (vim -es mangles line-crossing motions, so the comparisons were done in a real instance.)

CI is running on the new head — DCO, module integrity, and gofmt+license are already green; golangci-lint and the platform test legs are still in flight.

Comment thread internal/text/cursor.go
return !ok || c.wordClass(nextCell.Ch, bigWord) != class
}

func IsWordObjectBlank(r rune) bool {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

golangci-lint fails here: exported function IsWordObjectBlank should have comment or be unexported (revive). vi uses it, so it needs a doc comment that states the contract (what counts as blank, including the \x00 empty cell).

Comment thread internal/text/cursor.go Outdated
}

func isWordObjectRune(r rune) bool {
func IsWordObjectRune(r rune) bool {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Same revive error. IsWordObjectRune is only used inside cursor.go (by wordClass), so it can go back to being unexported.

The cw rework exported both helpers; IsWordObjectRune is only used
inside cursor.go by wordClass so it returns to unexported, while
IsWordObjectBlank is used by the vi package and gains a doc comment
stating what counts as blank: space, tab, or the \x00 empty cell.

Signed-off-by: Santhi Prakash <[email protected]>
@santhiprakash

Copy link
Copy Markdown
Contributor Author

Both fixed in 56773eca:

  • IsWordObjectBlank stays exported (vi uses it) and now carries a doc comment stating the contract — blank means space, tab, or the \x00 rune of an empty cell.
  • IsWordObjectRune is back to unexported isWordObjectRune; wordClass was its only caller.

Verified with the same command CI runs: golangci-lint run --timeout=600s (v2.14.0) reports 0 issues, and the golangci-lint leg on this push is green. go test ./internal/text/... passes locally; the two platform test legs are still running.

@ernestrc
ernestrc enabled auto-merge (squash) October 1, 2026 22:59
@ernestrc
ernestrc merged commit 53a9d36 into unstablebuild:main Oct 1, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Vim editor cw acts like dw + insert

3 participants