Skip to content

Fix VT sequences leaking as text when Esc is held - #1012

Merged
Leonard Hecker (lhecker) merged 3 commits into
microsoft:mainfrom
saiv0n:fix-esc-esc
Nov 24, 2026
Merged

Leonard Hecker (lhecker) merged 3 commits into
microsoft:mainfrom
saiv0n:fix-esc-esc

Conversation

@saiv0n

@saiv0n Umut (saiv0n) commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Closes #641

When Esc is held and another key sends a sequence, the input looks like ESC ESC [ A. Previously, the parser treated the second ESC as the final char of an ESC x sequence, input.rs dropped that Esc('\x1b') token, and [A then came through as plain text.

Now, per the VT100 state machine, receiving ESC while in State::Esc cancels the previous escape sequence and stays in State::Esc, so whatever follows (e.g. [A) is still parsed as a sequence instead of leaking as text.

@saiv0n

Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree

Comment thread crates/edit/src/vt.rs Outdated
}

#[cfg(test)]
mod tests {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you back out these tests from the PR? Otherwise: LGTM!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sure thing, just dropped the tests! Thanks for taking a look.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'd be interested in seeing more precedent for this. Line editors seem to treat the following ESC as cancelling the first (expected); visual editors like nano seem to treat it in the new way you are proposing. Midnight Commander seems to defer behavior to the shell (see "line editors" above).

Comment thread crates/edit/src/vt.rs
Comment on lines +208 to +213
'\x1b' => {
// ESC ESC: the first one was a lone Escape key press (e.g. while
// holding Esc). Stay in the Esc state so that whatever follows
// (e.g. `[A`) is still parsed as a sequence and not as text.
return Some(Token::Esc('\0'));
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I do not believe this is correct.

VT100:

This state is entered whenever the C0 control ESC is received. This will immediately cancel any escape sequence, control sequence or control string in progress. If an escape sequence or control sequence was in progress, “cancel” means that the sequence will have no effect

xterm's altSendsEscape and metaSendsEscape do not countermand this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Makes total sense, thanks for catching that! Updated it to just cancel the previous ESC and stay in State::Esc per VT100 instead of emitting Esc('\0'), so whatever follows still gets parsed as a sequence instead of leaking as text. Really appreciate the review!

@lhecker Leonard Hecker (lhecker) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks! (And thanks Dustin! Didn't know about that!)

@lhecker
Leonard Hecker (lhecker) merged commit 477a80a into microsoft:main Nov 24, 2026
6 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.

Holding Esc key causes vt-sequences to leak

3 participants