Skip to content

[Bug] multiline inserts control characters (Backspace at the start, Ctrl+key) #637

Closed

Description

@kuldeepsinh19

Environment

  • OS: Linux (Docker)
  • Node Version: v24.22.1
  • Package: @clack/core (through @clack/prompts)
  • Package Version: @clack/prompts 1.9.0, @clack/core 1.6.0, same on main (50c8ea0)

Describe the bug

In a multiline prompt, pressing Backspace with the cursor at the start of the input inserts a DEL character (\x7f) instead of doing nothing. Ctrl combinations such as Ctrl+A or Ctrl+U insert their control character too (\x01, \x15).

Terminals don't draw these characters, so the prompt looks unchanged apart from the placeholder disappearing, but they are part of the returned value. Holding Backspace to clear the field leaves a \x7f behind on every other keypress.

The key handler in MultiLinePrompt inserts any keypress that has a char (multi-line.ts#L120-L139). For named keys Node passes the raw sequence as char, so Backspace at cursor 0 skips the cursor > 0 branch and reaches the insert. DatePrompt already checks for '\x7f' and '\b' (date.ts#L273-L278).

To Reproduce

// repro.mjs, with "@clack/prompts": "1.9.0"
import { multiline } from '@clack/prompts';

const value = await multiline({ message: 'Notes', defaultValue: 'none' });
console.log(value.length, [...value].map((c) => c.charCodeAt(0)));
  • Run node repro.mjs.
  • Press Backspace, then Enter twice.

It prints 1 [ 127 ]: the value is a single DEL and defaultValue is not used.

More sequences, fed to the prompt as the raw bytes a terminal sends:

Keys Returned
Backspace, h, i, Enter, Enter "\u007fhi"
x, Ctrl+A, Ctrl+U, Enter, Enter "x\u0001\u0015"
h, i, Backspace ×3, o, k, Enter, Enter "\u007fok"

The README's if (value.length === 0) return 'value is required' check also accepts the Backspace-only answer, since its length is 1.

Expected behavior

4 [ 110, 111, 110, 101 ], i.e. "none". Backspace at the start does nothing, which is what the backspace does nothing at start test describes. It passes today because it emits '' as the character, while a terminal sends \x7f. Ctrl+key combinations shouldn't add characters either.

Additional Information

#529 mentioned "odd behavior when backspacing at the start of the line". I have a small fix with tests and will link the PR here.

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

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions