Skip to content

improve escape handling - #18217

Merged
fdncred merged 5 commits into
nushell:mainfrom
fdncred:improve_escaping
May 18, 2026
Merged

fdncred merged 5 commits into
nushell:mainfrom
fdncred:improve_escaping

Conversation

@fdncred

@fdncred fdncred commented May 16, 2026 •

Copy link
Copy Markdown
Contributor

Description

For years nushell has had "ok" escaping when used with double quotes and \ for escapes. Nushell wasn't necessarily bad, but it wasn't real good either. This PR updates parser escaping behavior when \ is used in double quotes or in string interpolation with double quotes. It also does some minor refactoring of the related parser internals for path-like values.

I also added quite a few regression tests to ensure proper functionality.

User-facing changes (Release notes)

Improved escape handling

Nushell handles escaping better with the back slash \ when use with double quotes or string interpolation with double quotes where escapes are interpreted.

Nushell now supports and validates more backslash escape forms consistently in string parsing, and reports clearer errors for invalid unicode/hex escapes (including better wording around missing } and unicode code point limits).

Parse and process escape sequences in a byte string.

Now, the following escape sequences are handled:

  • Simple: \n, \r, \t, \\, \", \'
  • Control: \0, \a, \b, \e, \f
  • Hex: \xHH (exactly 2 hex digits)
  • Unicode: \u{XXXXXX} (1-6 hex digits, max 0x10FFFF)
  • Special: \/, \(, \), \{, \}, \$, \^, \#, \|, \~

Additional notes

@github-actions github-actions Bot added the A:parser Issues related to parsing label May 16, 2026
@Bahex
Bahex self-requested a review May 17, 2026 12:57
Bahex added 3 commits May 17, 2026 17:35
avoids mutable variables, manual iteration and memory allocation
avoids manual iteration and memory allocation
Comment on lines +3015 to +3018
trace_name: &'static str,
error_msg: &'static str,
make_expr: F,
make_interp_expr: I,

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.

Better to wrap these into an enum?

@fdncred
fdncred merged commit cef0dc4 into nushell:main May 18, 2026
17 checks passed
@fdncred
fdncred deleted the improve_escaping branch May 18, 2026 12:28
@github-actions github-actions Bot added this to the v0.113.0 milestone May 18, 2026
fdncred pushed a commit that referenced this pull request May 19, 2026
Addresses [this
comment](#18217 (comment))

@fdncred Since it'a trivial task, I just vibe coded it in case you
didn't notice the comment or didn't get what I meant.
@fdncred fdncred added notes:ready Indicates Ready for Release notes notes:other Noted in "Other changes" section labels May 19, 2026
fdncred pushed a commit that referenced this pull request Sep 10, 2026
## Description

Terminals such as macOS Terminal.app, iTerm2 and Ghostty escape spaces
with a backslash when a
file or folder is dragged into the window, for example
`/Users/mac/Downloads/a\ hourly\ list\ 1.txt`. Nushell could not accept
that text anywhere: a bare
word splits at the space, and `"a\ b"` was a parse error (`unrecognized
escape sequence '\ '`).

In this PR, I added `\ ` (a backslash followed by a space) to the escape
sequences recognized
inside double-quoted strings. It yields a literal space and joins the
existing "special" escapes
(`\(`, `\)`, `\{`, `\}`, `\$`, `\#`, `\~`, `\|`, ...), which already
cover the other characters those
terminals escape. The workflow becomes: type `"`, drop the file, type
`"`.

Why double quotes only:

- `"a\ b"` was a hard error before, so no existing script changes
meaning, on any platform.
- Single quotes and backticks stay byte-literal. The NUON spec documents
backticks as literal, and
the path completer emits raw paths inside backticks without escaping
backslashes, so giving `\ `
a meaning there would be a breaking change (and would silently rewrite
`C:\dir\ file` on Windows).
- Bare-word support is not possible without breaking Windows paths such
as `ls C:\ D:\`.

Because `unescape_string` is shared, the escape works in `"..."`,
`$"..."`, external command
arguments, `source`/`use` paths and `from nuon`. No lexer, completion or
highlighting changes were
needed. The doc comment on `unescape_string` that was lost in the parser
file split (#18388) is
restored, and the NUON spec now lists the new escape.

```nushell
"/Users/mac/Downloads/a\ hourly\ list\ 1.txt"
# => /Users/mac/Downloads/a hourly list 1.txt

ls "dir\ with\ space/file\ one.txt" | get name
# => dir with space/file one.txt

'a\ b'       # => a\ b   (unchanged)
echo `a\ b`  # => a\ b   (unchanged)
```

## User-facing changes (Release notes)

Added `\ ` (a backslash followed by a space) as an escape for a literal
space inside double-quoted
strings, so paths that terminals escape on drag-and-drop can be pasted
between double quotes
unchanged: `ls "/Users/me/My\ Documents/notes\ (1).txt"`. Previously
this was a parse error.
Single quotes, backticks and bare words are unchanged.

## Additional notes

- Closes #12464 (the double-quoted form; bare-word `\ ` remains out of
scope because of Windows
  paths, as discussed in the issue).
- Related: #11944, #18217.
- Tests added in `tests/parsing/escaping.rs`: the reporter's exact path,
interpolation, the
`\\ ` ordering, proof that single quotes and backticks stay literal, an
end-to-end `ls`/`cd` on a
directory with a space, and an external command receiving a single
argument.
- The book's list of double-quote escapes (nushell.github.io, "Working
with strings") needs the
  new entry after merge.

🤖 The test cases and this description was generated with [Claude
Code](https://claude.com/claude-code)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A:parser Issues related to parsing notes:other Noted in "Other changes" section notes:ready Indicates Ready for Release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support escape space by backslash in command line nushell doesn't handle escaped paths from drag-and-drop on macos

3 participants