Rewriting prompts with hooks lets you create custom shortcuts and transform simple commands into complex instructions. Use TypeScript to intercept, modify, and replace prompts before Claude sees them.
Read stdin, modify, and output:
import { type UserPromptSubmitHookInput } from "@anthropic-ai/claude-code"
const input = await Bun.stdin.json() as UserPromptSubmitHookInput
// Transform any input into a joke request
console.log(`Make a joke about ${input.prompt}`)
Test it:
claude
Pizza
# Output: "Why did the pizza maker go to therapy? He had too many toppings to deal with!"
Add conditional logic for specific patterns:
import { type UserPromptSubmitHookInput } from "@anthropic-ai/claude-code"
const input = await Bun.stdin.json() as UserPromptSubmitHookInput
if (input.prompt.endsWith(":plan")) {
// Strip :plan and rewrite
const task = input.prompt.replace(/:plan$/, "")
console.log(`Create a detailed step-by-step plan for: ${task}`)
}
// No log = original prompt passes through
Usage:
# Regular prompt - unchanged
> That's funny
# Custom syntax - transformed
> build a CLI app :plan
# Becomes: "Create a detailed step-by-step plan for: build a CLI app"
:json - Wrap response in JSON format:test - Add "write tests for" prefix:explain - Transform to ELI5 explanation/v(5) - Generate N variations (see lesson 13)Prompts:
pizza
hello world cli :plan
[00:00] So now that we have our user prompt submit trigger on every user prompt, anything you console log at the end of your hook can be used to essentially rewrite the prompt. So I can say make a joke about the input, and now when I launch clod here and I say pizza, you'll see that it makes a joke about pizza. So you're essentially intercepting the original prompt and then providing a new one. And this can give you some really interesting options like if the input prompt ends with, and then you can come up with some syntax for your project like colon plan, then we'll intercept the prompt and we'll now log out, create a plan for our original prompt. So now if I enter that's funny you'll see it doesn't create a plan, but if I say a hello world CLI colon plan hit enter you'll now see a I'll create a plan for building a hello world CLI application.