Skip to content

[BUG] A raw ESC inside a bracketed paste turns the rest of the paste into Key events (Enter included) #6802

Backlog

Description

@AnmolMajithia

Have you checked closed issues? Yes. I searched open and closed issues for "paste" and found nothing on this.

Have you checked against the most recent version of Textual? Yes: 8.2.8, the latest on PyPI. src/textual/_xterm_parser.py on main is the same apart from a docstring typo fix.

The bug

Pasted text can contain a raw ESC (\x1b) inside a bracketed paste (\x1b[200~ … \x1b[201~). When it does, XTermParser doesn't deliver the paste whole. The text after the ESC comes out as Key events, an enter key included for a newline, and those keys arrive before the Paste event. The Paste then holds only the text before the ESC.

So in an app that uses Input, TextArea or a key binding, pasting such text can:

  • press Enter, which submits a half-written value;
  • trigger bindings.

An app that forwards keys to a child program, such as a terminal widget, presses those keys in the child.

Example

It runs as is: pip install textual==8.2.8, then python repro.py.

"""A bracketed paste with a raw ESC inside is split into key events (Textual 8.2.8)."""
import textual
from textual import events
from textual._xterm_parser import XTermParser


def run(label: str, data: str) -> None:
    parser = XTermParser()
    messages = list(parser.feed(data)) + list(parser.feed(""))
    shown = [
        f"Paste({m.text!r})" if isinstance(m, events.Paste)
        else f"Key({m.key!r})" if isinstance(m, events.Key)
        else type(m).__name__
        for m in messages
    ]
    print(f"{label}:\n  input  {data!r}\n  events {shown}\n")


print("textual", textual.__version__, "\n")
run("control: paste without ESC", "\x1b[200~hello\rworld\x1b[201~")
run("paste with a raw ESC, then Enter", "\x1b[200~hello\x1bworld\rmore\x1b[201~")
run("ESC followed by text and the closer", "\x1b[200~ab\x1bcd\x1b[201~")

The output:

textual 8.2.8

control: paste without ESC:
  input  '\x1b[200~hello\rworld\x1b[201~'
  events ["Paste('hello\\rworld')"]

paste with a raw ESC, then Enter:
  input  '\x1b[200~hello\x1bworld\rmore\x1b[201~'
  events ["Key('circumflex_accent')", "Key('w')", "Key('o')", "Key('r')", "Key('l')", "Key('d')", "Key('enter')", "Key('m')", "Key('o')", "Key('r')", "Key('e')", "Paste('hello')"]

ESC followed by text and the closer:
  input  '\x1b[200~ab\x1bcd\x1b[201~'
  events ["Key('circumflex_accent')", "Key('c')", "Key('d')", "Paste('ab')"]

The expected result for the second case is one event, Paste('hello\x1bworld\rmore'), or the same with the ESC stripped.

The same happens when the text after the ESC is longer than _MAX_SEQUENCE_SEARCH_THRESHOLD (32), even with no second ESC: with "\x1b[200~a\x1b" + "x" * 37 + "\x1b[201~", 33 characters come out as Key events and the rest as the Paste.

Where it happens

In _xterm_parser.py (8.2.8), XTermParser.parse:

  • Only the outer loop appends to the paste buffer (if bracketed_paste: paste_buffer.append(character), lines 218–219).
  • A raw ESC inside the paste starts the escape-sequence loop. The characters read there go into sequence, not paste_buffer.
  • That loop calls send_sequence() / reissue_sequence_as_keys() even while bracketed_paste is true. It does so when:
    • the next ESC arrives, which is usually the paste's own \x1b[201~ (lines 250–251);
    • ESCAPE_DELAY times out (lines 242–243);
    • the sequence passes _MAX_SEQUENCE_SEARCH_THRESHOLD (lines 256–257).

A possible fix: while bracketed_paste is true, don't reissue anything as keys. Keep every character, ESC included, in the paste buffer until the exact \x1b[201~ arrives, then emit one Paste.

Environment

textual diagnose isn't available here (no textual-dev). The versions are Textual 8.2.8 and Python 3.13.18, on macOS 27.2.1 arm64. The parser logic doesn't depend on the platform.


We hit this in rlx, an open-source terminal UI built on Textual, where a paste can reach an agent's terminal pane as keystrokes. The report and the repro were prepared with help from Claude.

Activity

  1. github-actions commented on Nov 27, 2026

    @github-actions

    We found the following entries in the FAQ which you may find helpful:

    Feel free to close this issue if you found an answer in the FAQ. Otherwise, please give us a little time to review.

    This project is developed and maintained by Will McGugan. Consider sponsoring Will's work on this project (and others).

    This is an automated reply, generated by FAQtory

  2. tony1233200 commented on Nov 27, 2026

    @tony1233200

    Proposed solution, posted for maintainer sign-off as required by AI_POLICY.md: while a bracketed paste is in progress, XTermParser.parse should never reissue an in-progress escape sequence as key events. On the next-ESC, timeout, EOF, and over-threshold paths the sequence stays as literal paste content; a nested paste-start or focus sequence inside a paste is also literal; only the exact \x1b[201~ ends the paste, which is then delivered as a single Paste event. Non-paste paths are unchanged.

    I have a tested patch with regression tests ready on a fork branch (fix-6802-bracketed-paste-esc) and can open an AI-identified PR as soon as the approach is approved.

    (Prepared with AI assistance, agent: Muse.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions