name: file-writer description: Safe file writing strategy for large files. Use when you need to write or update large files (over 5000 bytes) to avoid truncation. Provides step-by-step approach: (1) Read current state, (2) Use edit tool for precise modifications, (3) Verify results, (4) Repeat until complete. Also provides fallback strategies and error recovery.
Safe file writing strategy for large files to avoid truncation issues.
Large file writes can fail due to:
write tool may have byte limits per operation| Operation | Safe Size | Risk Level |
|---|---|---|
write new file |
< 2000 bytes | ✅ Safe |
write new file |
2000-5000 bytes | ⚠️ Moderate |
write new file |
> 5000 bytes | ❌ High risk |
edit modification |
< 500 bytes | ✅ Safe |
edit modification |
500-1000 bytes | ⚠️ Moderate |
edit modification |
> 1000 bytes | ❌ High risk |
Before making changes, read the current file state:
# Read the file to understand current structure
read /path/to/file.md
# For large files, read specific sections
read /path/to/file.md --offset 100 --limit 50
Purpose: - Understand file structure - Identify exact text to replace `- Determine modification points
For small to medium changes, use edit tool:
Use `edit` tool with:
- `oldText`: Exact text to replace (must match exactly)
- `newText`: New text to replace with
- `file_path`: Path to the file
Best Practices:
- Keep modifications under 500 bytes
- Match oldText exactly (including whitespace)
- Use unique text markers for large sections
- Verify oldText exists before editing
After each modification, verify the result:
# Check file line count
wc -l /path/to/file.md
# Read modified section
read /path/to/file.md --offset <start> --limit <lines>
# Verify file ends correctly
read /path/to/file.md --offset -10
Success Criteria: - File size increased as expected - Modified section contains new content - File ends correctly (not truncated) - No syntax errors (for code files)
For large multi-section updates:
If write tool fails for large content:
Split content into chunks:
1. Write first chunk (header + section 1)
2. Verify and append section 2
3. Verify and append section 3
4. Continue until complete
For complex modifications, use unique markers:
In source file:
<!-- SECTION_START: configuration -->
[existing content]
<!-- SECTION_END: configuration -->
Edit operation:
oldText: "<!-- SECTION_START: configuration -->\n[existing content]\n<!-- SECTION_END: configuration -->"
newText: "<!-- SECTION_START: configuration -->\n[new content]\n<!-- SECTION_END: configuration -->"
For creating new large files:
1. Create file with basic structure
2. Add sections one by one using edit
3. Verify after each addition
4. Final verification
For critical file modifications:
# Create backup before modification
cp /path/to/file.md /path/to/file.md.backup
# Attempt modification
# ... (write or edit operation)
# If modification fails, restore
cp /path/to/file.md.backup /path/to/file.md
小葱技能7w4.net有完整的技能分类。
Symptom: "Could not find exact text in file"
Causes:
- oldText doesn't match exactly
- Whitespace differences (spaces vs tabs)
- File already modified
- Text doesn't exist in file
Solutions:
oldText for more precise matchingSymptom: File ends abruptly or is incomplete
Solutions:
Symptom: File has syntax errors or invalid content
Solutions:
Step 1: Read file to find insertion point
read /path/to/large-file.md --offset 50 --limit 10
Step 2: Use edit to add new section
edit:
file_path: /path/to/large-file.md
oldText: "## Existing Section\n\nContent here"
newText: "## Existing Section\n\nContent here\n\n## New Section\n\nNew content"
Step 3: Verify
read /path/to/large-file.md --offset 50 --limit 20
Step 1: Create basic structure
write:
file_path: /path/to/new-file.md
content: "# Title\n\n## Section 1\n\nContent 1"
Step 2: Add sections incrementally
edit:
file_path: /path/to/new-file.md
oldText: "Content 1"
newText: "Content 1\n\n## Section 2\n\nContent 2"
Step 3: Verify
wc -l /path/to/new-file.md
Step 1: Read file to find exact text
read /path/to/file.md
Step 2: Use unique markers
edit:
file_path: /path/to/file.md
oldText: "<!-- START: old-section -->\n[old content]\n<!-- END: old-section -->"
newText: "<!-- START: old-section -->\n[new content]\n<!-- END: old-section -->"
Step 3: Verify
read /path/to/file.md | grep "new content"
Need to write/update file?
├─ New file?
│ ├─ < 2000 bytes?
│ │ └─ Use write tool
│ └─ > 2000 bytes?
│ └─ Use incremental build strategy
└─ Existing file?
├─ Small change (< 500 bytes)?
│ └─ Use edit tool
├─ Medium change (500-1000 bytes)?
│ └─ Use edit with unique markers
└─ Large change (> 1000 bytes)?
└─ Use incremental edit strategy
| Task | Tool | Strategy |
|---|---|---|
| Create small file | write | Direct write |
| Create large file | write + edit | Incremental build |
| Small modification | edit | Direct edit |
| Medium modification | edit | Edit with markers |
| Large modification | edit | Incremental edit |
| Replace section | edit | Unique markers |
| Append content | edit | Edit end of file |
这是一个帮助处理大文件写入问题的 Skill,实用性较好。它清楚地解释了文件为什么会写入失败,并给出了具体的解决步骤和备选方案,内容覆盖面较全。优点是提供了详细的安全大小参考和多种回退策略;不足之处在于缺少实际使用示例,部分说明略显冗长,整体内容偏向理论指导,对于需要快速上手的用户来说可能不够直观。总体质量中规中矩,适合作为参考指南使用。