heredoc-file-write
它能做什么
这项技能为 AI 提供一种备用的代码文件保存方法。当普通文件写入功能因引号、转义符或其他特殊字符反复出错时,就使用它。适合让 AI 生成 React、TypeScript 或 JSON 文件的人。
适用场景
- ✓当文件写入功能在同一个代码文件上反复失败、且报错原因不明时。
- ✓当代码文件中包含嵌套引号、转义字符或其他复杂语法时。
- ✓当我需要从 shell 中可靠地创建代码文件时。
技能链接
https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/heredoc-file-write/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请帮我:文件写入功能因为引号问题,一直无法保存我的 React 组件文件。请换一种方式保存。”
每周 5 个新技能,1 分钟读完。
工作原理
AI 使用带引号标记的 cat 命令,原样写入代码,shell 不会改动其中的任何字符。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Heredoc File Write Workaround
When to Use This Skill
Use this pattern when:
write_filerepeatedly fails with "unknown error" or similar messages- The target file contains complex code (TypeScript, JSX, JSON with special characters, etc.)
- Standard file writing approaches encounter encoding or escaping issues
- You need a reliable fallback method to create files
Core Technique
Switch from write_file to run_shell using heredoc syntax with cat. The heredoc approach handles special characters, quotes, and complex content more reliably.
Step-by-Step Instructions
Step 1: Identify Write Failures
When write_file fails repeatedly on the same file:
- Note the error message (e.g., "unknown error", encoding issues)
- Review the file content for special characters, nested quotes, or complex syntax
- Attempt the heredoc workaround below
Step 2: Use Heredoc Syntax
Execute shell command with heredoc:
cat > /path/to/file.tsx << 'EOF'
// Your file content here
import React from 'react';
const Component = () => {
return <div>Hello</div>;
};
export default Component;
EOF
Step 3: Key Heredoc Rules
Use quoted delimiter (
'EOF'notEOF) to prevent variable expansion:'EOF'= literal content (recommended for code files)EOF= allows variable expansion (use only if needed)
Match delimiters exactly: Opening and closing
EOFmust be on their own lines with no leading/trailing whitespaceNo escaping needed: Content between delimiters is taken literally when using quoted delimiter
Step 4: Verify File Creation
After executing the heredoc command:
- Check if file exists:
ls -la /path/to/file.tsx - Verify content:
cat /path/to/file.tsxorhead -20 /path/to/file.tsx - Proceed with subsequent tasks once confirmed
Example Scenarios
TypeScript/JSX Component
cat > src/components/Button.tsx << 'EOF'
import React from 'react';
interface ButtonProps {
label: string;
onClick: () => void;
}
export const Button: React.FC<ButtonProps> = ({ label, onClick }) => {
return (
<button onClick={onClick} className="btn-primary">
{label}
</button>
);
};
EOF
JSON Configuration
cat > tsconfig.json << 'EOF'
{
"compilerOptions": {
"target": "ES2020",
"module": "commonjs",
"strict": true,
"esModuleInterop": true
},
"include": ["src/**/*"]
}
EOF
Complex Shell Script
cat > deploy.sh << 'EOF'
#!/bin/bash
set -e
echo "Deploying..."
if [ "$ENV" = "production" ]; then
echo "Production deployment"
fi
EOF
Advantages Over write_file
- No escaping complexity: Quotes, backslashes, and special characters work naturally
- Preserves formatting: Indentation and line breaks remain exact
- Handles multi-line content: No need to concatenate strings or use base64
- More reliable: Shell handles file I/O directly with fewer abstraction layers
Limitations
- Creates files in shell execution context (ensure correct working directory)
- Less programmatic than
write_filefor dynamic content generation - Requires shell access (may not work in restricted environments)
Troubleshooting
Problem: File not created at expected path
- Solution: Use absolute paths or verify current working directory with
pwd
Problem: Content appears corrupted
- Solution: Ensure delimiter is quoted (
'EOF') and closing delimiter has no trailing whitespace
Problem: Permission denied
- Solution: Ensure target directory exists and you have write permissions
常见问题
为什么要给标记加引号?
加上引号后,shell 就不会改动代码中的美元符号或其他符号。
文中提到了哪些代码类型?
指南提到了包含特殊字符的 TypeScript、JSX 和 JSON 文件。
结束标记应该怎么写?
结束标记必须与开始标记完全一致,并单独占一行。
详情
作者的原始描述
Use shell heredoc syntax as a workaround when write_file fails on complex TypeScript/JSX/code files with encoding or escaping issues
repo: HKUDS/AgentPilot
热度
- GitHub 星标
- 7.8k
- 排名
- #12891
- 许可证
- MIT
- 更新时间
- 2026/11/1
评论
- …
同一仓库的更多技能
agentpilot
7.8k帮你的智能体准备任务并运行 AgentPilot 循环,逐步改进项目:循环会提出修改、测试修改并为其打分。
openspace
7.8k帮你的智能体设置 OpenSpace,这是一个用于存储和获取智能体技能的工具,并通过 MCP 将其连接到你的 AI 应用。
rsi-arena
7.8k帮你的智能体设置并运行标准 AI 基准测试,例如 AIME 和 Terminal-Bench,用来比较不同的改进方法。
experiment-guide
7.8k帮你的智能体规划小型实验、阅读实验结果并记录发现,同时研究如何改进一个程序。