rtk-ai/rtk

Feature: rewrite subcommands inside multi-line / heredoc Bash blocks

Open

#1.243 aberto em 12 de abr. de 2026

Ver no GitHub
 (2 comments) (1 reaction) (0 assignees)Rust (2.914 forks)batch import
area:clieffort-largeenhancementhelp wantedpriority:medium

Métricas do repositório

Stars
 (48.085 stars)
Métricas de merge de PR
 (Mesclagem média 11d 1h) (45 fundiu PRs em 30d)

Description

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:

  1. Line-by-line rewrite for multi-line blocks: split on \n, rewrite each line independently, rejoin
  2. Statement-level rewrite for ;-separated commands
  3. 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)

Guia do colaborador