Skip to content

fix: backspace once per character when replacing wide-char tokens - #487

Closed
aaaxingiiiiiii wants to merge 1 commit into
microsoft:mainfrom
aaaxingiiiiiii:fix/wide-char-backspace
Closed

aaaxingiiiiiii wants to merge 1 commit into
microsoft:mainfrom
aaaxingiiiiiii:fix/wide-char-backspace

Conversation

@aaaxingiiiiiii

Copy link
Copy Markdown

Summary

calculateReplacement sends tokenLength backspaces before inserting a suggestion, but tokenLength is a display width, where a CJK character counts as 2. Shells delete one character per backspace, so tokens that contain wide characters delete too much.

For example, type cd 中 and accept the folder 中文目录/. Two backspaces are sent for one character, so the space after cd is also deleted and the line becomes cd中文目录/.

This change converts the display width to a character count before sending backspaces. Quotes and escape characters are single-width, so the difference between the token text's display width and its character count is exactly the extra width.

The existing test wide-char divergent uses display width for backspace expected the old behavior. I renamed it and changed it to expect one backspace, and added a mixed-width case.

Test plan

  • Ran a node-pty session with the bash and zsh that ship with macOS. Before the fix, the preceding character was deleted. After the fix, the line is correct.
  • src/tests/runtime/replacement.test.ts passes.
  • npm run build and npm run lint pass.
  • I have not yet verified PSReadLine. I expect it to behave the same way, since it also deletes one character per backspace.

🤖 Generated with Claude Code

calculateReplacement used tokenLength, a display width, as the number of
backspaces to send. Shells delete one character per backspace, so a
token containing wide characters (CJK) over-deleted: typing `cd 中` and
accepting `中文目录/` sent two backspaces for one character and also
removed the space before it, producing `cd中文目录/`.

Convert the display width to a character count before sending
backspaces. Quotes and escape characters are single-width, so the
difference between the token text's display width and its character
count is exactly the extra width to subtract.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@aaaxingiiiiiii
aaaxingiiiiiii deleted the fix/wide-char-backspace branch October 15, 2026 06:37
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.

1 participant