Feature: rewrite subcommands inside multi-line / heredoc Bash blocks
#1,243 建立於 2026年4月12日
倉庫指標
- Star
- (48,085 star)
- PR 合併指標
- (平均合併 11天 1小時) (30 天內合併 45 個 PR)
描述
Context
I'm using the RTK Claude Code hook (rtk-rewrite.sh v3) with RTK 0.35.0. The hook works well for single-line commands, but many real-world Claude Code sessions generate multi-line Bash blocks that contain rewritable subcommands which currently pass through unrewritten.
Problem
rtk rewrite treats the entire Bash input as a single command string. When Claude Code generates compound or multi-line commands, RTK either:
- Returns exit 1 (no equivalent) for the whole block, even though individual lines inside are rewritable
- Misses optimization opportunities on the most token-heavy commands (heredocs with surrounding setup)
Examples from real sessions
Heredoc Python blocks (most common — 16 occurrences in 30 days)
python3 << 'PYEOF'
import json
# ... 40 lines of Python ...
PYEOF
Not rewritable as a whole, but the output could still benefit from RTK filtering.
For-loops with rewritable inner commands
for f in *.sh; do ls -la "$f"; done
Exit 1 — but ls -la inside is a known RTK command.
Sequential commands with ; separator
cd /some/path; ls -la; grep -r "pattern" .
The ls -la and grep -r parts are individually rewritable.
What works today
Compound commands with && and pipes are handled (exit 0):
ls -la . && echo done # ✅ rewritten
ls -la ~/.claude/ | head -5 # ✅ rewritten
cd /tmp && ls -la # ✅ rewritten
Proposal
Consider parsing multi-line and compound Bash inputs to identify and rewrite individual subcommands where possible. Possible approaches:
- Line-by-line rewrite for multi-line blocks: split on
\n, rewrite each line independently, rejoin - Statement-level rewrite for
;-separated commands - Output-only filtering for heredoc blocks: even if the command can't be rewritten, the output could be filtered through RTK's token reduction
Impact estimate
From rtk discover on my setup:
- 175 Bash commands over 30 days
- 91 rewritable commands identified → ~11.9K tokens saveable
- 16 Python heredocs are the single largest category of unrewritable commands
Environment
- RTK 0.35.0
- Claude Code with Opus 4.6 (1M context)
- macOS Darwin 25.4.0
- Hook:
rtk-rewrite.sh(PreToolUse Bash, hook-version 3)