Install
openclaw skills install file-writerSafely write or update large files over 5000 bytes by reading, incrementally editing small sections, verifying each change, and using fallback recovery methods.
openclaw skills install file-writerSafe 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:
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:
oldText exactly (including whitespace)oldText exists before editingAfter 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:
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
Symptom: "Could not find exact text in file"
Causes:
oldText doesn't match exactlySolutions:
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 |