Overview
The Write tool creates new files or completely replaces existing file contents. It’s designed for file creation and complete rewrites. For surgical edits to existing files, use the Edit tool instead.Parameters
The path to the file to create or overwrite. Can be absolute or relative to the working directory.
The complete content to write to the file. This replaces any existing content entirely.
Features
Automatic Directory Creation
Write automatically creates parent directories if they don’t exist:/path/to/new/directory/ doesn’t exist, it will be created with permissions 0755.
Overwrite Protection
Write checks if a file was modified since last read:- Tracks last read time via filetracker
- Compares modification time with last read time
- Returns error if file was modified externally
- Prevents accidental overwrites of manual changes
Content Deduplication
Write checks if the new content is identical to existing content:Diff Generation
Write generates a unified diff showing the changes:Usage Examples
Create New File
Overwrite Existing File
Create Multiple Files
Generate Code
Best Practices
When to Use Write
Use Write for:- Creating new files
- Generating code
- Complete file rewrites
- Files where most content changes
- Template-based file creation
- Small files that are easy to regenerate
- Surgical changes to existing files
- Replacing specific functions
- Modifying configuration values
- Adding/removing specific sections
- Files where small portions change
File Creation Workflow
- Use LS tool to verify parent directory exists
- Prepare complete content including imports, comments, etc.
- Use Write tool to create the file
- Use View tool to verify the result
- Check LSP diagnostics in the response
File Overwrite Workflow
- Use View tool to read current content
- Prepare new complete content
- Use Write tool to overwrite
- Check response for errors or diagnostics
- Use View tool to verify if needed
Content Preparation
- Include all necessary imports at the top
- Add file-level comments if appropriate
- Use correct indentation (tabs or spaces)
- End with newline for compatibility
- Follow project conventions for style
Permissions
Write requires permission for all operations. You can pre-approve Write for specific paths:- Generated code directories
- Temporary files
- Output directories
- Build artifacts
LSP Integration
After writing a file, Crush automatically:- Notifies LSP servers about the file change
- Waits for diagnostics (errors, warnings)
- Includes diagnostics in the response:
Error Handling
File Modified Since Last Read
Path is a Directory
Permission Denied
- Approve the permission prompt
- Add path to allow list in crush.json
- Check file system permissions
Parent Directory Doesn’t Exist
This shouldn’t happen as Write creates parent directories automatically, but if it does: Solution: Check file system permissions and disk space.Platform Notes
Line Endings
- Unix/Linux/macOS: LF (
\n) - Windows: CRLF (
\r\n)
- Use
\nin your content string - Write converts to appropriate line ending for the platform
File Permissions
- Unix/Linux/macOS: Files created with
0644(rw-r—r—) - Windows: Default Windows permissions
Path Separators
Use forward slashes/ on all platforms:
- ✅
C:/Users/name/file.txt - ❌
C:\\Users\\name\\file.txt
Advanced Usage
Template-Based Generation
Generate files from templates:Batch File Creation
Create multiple related files in parallel:Code Generation
Generate code from existing files:- Read source files with View
- Parse or analyze content
- Generate new code based on analysis
- Write generated files with Write tool
Write vs Edit vs MultiEdit
Use Write When:
- ✅ Creating new files
- ✅ Complete file rewrites
- ✅ Generated code
- ✅ Most of the file is changing
- ✅ Small files easy to regenerate
Use Edit When:
- ✅ Targeted changes to existing files
- ✅ Replacing specific functions
- ✅ Modifying one section
- ✅ Need precise control
- ✅ File is large or complex
Use MultiEdit When:
- ✅ Multiple changes to same file
- ✅ Several edits in sequence
- ✅ Each edit is surgical
- ✅ Want atomic operation
- ✅ Changes are independent