跳到正文
Skillpedia
通用编程7.8k · #12891

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。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/heredoc-file-write/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“请帮我:文件写入功能因为引号问题,一直无法保存我的 React 组件文件。请换一种方式保存。”

每周 5 个新技能,1 分钟读完。

工作原理

AI 使用带引号标记的 cat 命令,原样写入代码,shell 不会改动其中的任何字符。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Heredoc File Write Workaround

When to Use This Skill

Use this pattern when:

  • write_file repeatedly 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:

  1. Note the error message (e.g., "unknown error", encoding issues)
  2. Review the file content for special characters, nested quotes, or complex syntax
  3. 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

  1. Use quoted delimiter ('EOF' not EOF) to prevent variable expansion:

    • 'EOF' = literal content (recommended for code files)
    • EOF = allows variable expansion (use only if needed)
  2. Match delimiters exactly: Opening and closing EOF must be on their own lines with no leading/trailing whitespace

  3. No escaping needed: Content between delimiters is taken literally when using quoted delimiter

Step 4: Verify File Creation

After executing the heredoc command:

  1. Check if file exists: ls -la /path/to/file.tsx
  2. Verify content: cat /path/to/file.tsx or head -20 /path/to/file.tsx
  3. 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

  1. No escaping complexity: Quotes, backslashes, and special characters work naturally
  2. Preserves formatting: Indentation and line breaks remain exact
  3. Handles multi-line content: No need to concatenate strings or use base64
  4. 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_file for 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

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能