Skip to content

Keep backslashes in the config file and BAT_OPTS on Windows - #4164

Merged
keith-hall merged 2 commits into
sharkdp:masterfrom
GuangminJu:fix/windows-config-backslashes
Nov 29, 2026
Merged

keith-hall merged 2 commits into
sharkdp:masterfrom
GuangminJu:fix/windows-config-backslashes

Conversation

@GuangminJu

Copy link
Copy Markdown
Contributor

Problem

#4104 stopped shell_words from eating the backslashes in BAT_PAGER / PAGER / --pager
on Windows. The config file and BAT_OPTS still go through shell_words::split, so the
same value written there is still broken. With a config file containing

--pager=C:\Windows\System32\more.com

bat prints

[bat warning]: Pager 'C:WindowsSystem32more.com' not found, outputting to stdout instead

while BAT_PAGER=C:\Windows\System32\more.com works.

Reproduction (Windows 11, Git Bash, current master c758b4d)

$ printf '%s\n' '--pager=C:\Windows\System32\more.com' > config
$ BAT_CONFIG_PATH=$PWD/config bat --paging=always t.txt
[bat warning]: Pager 'C:WindowsSystem32more.com' not found, outputting to stdout instead
hello
$ BAT_PAGER='C:\Windows\System32\more.com' bat --no-config --paging=always t.txt
hello
                      # (more.com ran; it appends the blank line)

BAT_OPTS='--pager=C:\Windows\System32\more.com' shows the same problem as the config file.

Root cause

Each line of the config file (and the BAT_OPTS value) is split by get_args_from_str in
src/bin/bat/config.rs with shell_words::split. That function follows POSIX rules, where \
escapes the next character outside of quotes, so C:\tools\less.exe becomes C:toolsless.exe
before the pager code sees it. #4104 fixed this in pager::split_command, but the config file
is split earlier and separately.

Fix

The config file and BAT_OPTS are now split by the same function as the pager command
(pager::split_command), exposed to the binary as bat::config::split_arguments. It is gated
the same way as bat::config::get_pager_executable. On Windows, \ stays literal and single
and double quotes still group words. On other platforms nothing changes, because
split_command still calls shell_words::split there.

On Windows, the change is the same one #4104 made for BAT_PAGER. A backslash outside quotes
is no longer an escape: a\ b gives two words, and \" no longer gives a literal quote.
Quoting ("a b", 'a"b') still works. A config that worked around the bug by doubling
backslashes (C:\\tools\\less.exe) still works, because Windows treats the doubled
separators as one.

Tests

  • New unit test config::windows_paths_keep_backslashes (#[cfg(windows)], in
    src/bin/bat/config.rs). It fails without the fix:
    assertion `left == right` failed
      left: ["--pager=C:\\tools\\less.exe", "--paging=always"]
     right: ["--pager=C:toolsless.exe", "--paging=always"]
    
    and passes with it.
  • cargo test --locked on Windows 11 (x86_64-pc-windows-msvc): all suites pass
    (lib 158, bin 11, integration 290 passed / 2 ignored, snapshot 27, doc tests 3 passed / 2 ignored).
  • cargo fmt -- --check: clean.
  • cargo clippy --locked --all-targets --all-features -- -D warnings: exit 0, no warnings.
  • cargo check --locked --lib --no-default-features --features regex-onig: exit 0 (one existing dead-code warning in src/printer.rs, not from this change).
  • Manual check after the fix: the reproduction above runs more.com without the warning,
    for both the config file and BAT_OPTS.

CHANGELOG

Added an entry under "Bugfixes".

AI disclosure

This change was prepared with Claude Code (an AI coding assistant), which reproduced the bug and ran the tests above on my Windows machine. I reviewed the change before submitting.

🤖 Generated with Claude Code

GuangminJu and others added 2 commits November 29, 2026 18:55
The config file and BAT_OPTS were split with shell_words, which treats
a backslash as an escape, so --pager=C:\tools\less.exe became
C:toolsless.exe. Split them with the same function as the pager command,
which keeps backslashes on Windows since sharkdp#4104.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@keith-hall keith-hall left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks

@keith-hall
keith-hall merged commit c47644f into sharkdp:master Nov 29, 2026
24 checks passed
cindykrafft pushed a commit to cindykrafft/bat that referenced this pull request Nov 29, 2026
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.

2 participants