Skip to main content
Inline Editing provides surgical, AI-powered code modifications using the replace_in_file tool. Make targeted changes to specific functions, variables, or code sections without rewriting entire files.
Inline editing in action

How It Works

Inline Editing uses a SEARCH/REPLACE block pattern for precise modifications:
The AI:
  1. Searches for the exact code block in the file
  2. Replaces it with the modified version
  3. Preserves surrounding code untouched
  4. Handles auto-formatting automatically
Inline Editing is powered by the replace_in_file tool, designed for efficiency and precision.

When to Use Inline Editing

Inline Editing excels at:

Function Updates

Modify function implementations, signatures, or logic

Variable Renames

Change variable names throughout a file

Import Statements

Add, remove, or modify imports

Type Annotations

Add TypeScript types to existing code

Bug Fixes

Fix specific issues without touching working code

Refactoring

Update patterns across a file
For major restructuring or creating new files, use write_to_file instead. See Inline Editing vs write_to_file.

SEARCH/REPLACE Syntax

Single Block

Modify one section of code:

Multiple Blocks

Make several changes in one operation:
Best Practice: Use a single replace_in_file call with multiple SEARCH/REPLACE blocks rather than multiple separate calls.
See: src/core/prompts/system-prompt/components/editing_files.ts:75

Inline Editing vs write_to_file

Use when:
  • Making small, localized changes
  • Updating specific functions or sections
  • File content is mostly unchanged
  • You need surgical precision
Advantages:
  • More efficient (no need to send full file)
  • Lower chance of errors
  • Preserves unchanged sections perfectly
  • Safer for large files
Example use cases:
  • Fix a bug in one function
  • Add a new import
  • Update variable names
  • Modify configuration values
See: src/core/prompts/system-prompt/components/editing_files.ts:59

Auto-Formatting Handling

Inline Editing intelligently handles editor auto-formatting:
1

Initial Edit

AI makes the change using replace_in_file.
2

Editor Formats

Your editor (Prettier, ESLint, etc.) auto-formats the file:
  • Adjusts indentation
  • Converts quotes
  • Organizes imports
  • Adds/removes semicolons
3

AI Receives Updated State

The tool response includes the final formatted content:
4

Subsequent Edits Adapt

Future edits use the formatted version as reference.
Critical: The AI must use the post-formatting content for subsequent SEARCH blocks. This is especially important when making multiple edits to the same file.
See: src/core/prompts/system-prompt/components/editing_files.ts:5

Auto-Formatting Examples

Common transformations your editor might apply:
Before (AI writes):
After (Editor formats with 2 spaces):
Before:
After (Prettier with single quotes):
Before:
After (Sorted and grouped):
Before:
After (With trailing commas):

Best Practices

Be Specific in SEARCH Blocks

Problem: Might match multiple locations or wrong variable.

Include Surrounding Context

Problem: Many functions might have this line.

Handle Edge Cases

If a file has similar code blocks, include unique identifiers:
The comment makes it unique from other validate functions.
Avoid SEARCH blocks with timestamps or IDs:❌ <SEARCH>const id = 12345</SEARCH>✅ <SEARCH>const id = userId // Current user</SEARCH>
Match exact whitespace in SEARCH blocks:

Workflow Tips

1

Assess Scope

Before editing, determine if changes are localized or widespread.
2

Choose Tool

Use replace_in_file for targeted edits, write_to_file for major changes.
3

Stack Multiple Edits

Combine related changes into a single replace_in_file call:
4

Verify Changes

Review the diff after each edit to ensure correctness.
5

Use Updated Content

For subsequent edits, reference the final formatted state from previous operations.
See: src/core/prompts/system-prompt/components/editing_files.ts:71

Advanced Techniques

Multi-Line Replacements

Replace entire blocks while preserving structure:

Conditional Logic Updates

Modify complex conditional statements:

Pattern Replacements

Change recurring patterns:
Repeat for each occurrence in the file.

Common Use Cases

Add TypeScript Types

Update Imports

Fix ESLint Issues

Refactor Function Names

Error Handling

Search Block Not Found

Error: SEARCH block not found in file Causes:
  • Whitespace mismatch
  • File was auto-formatted since last read
  • Searching for old content
Solution:
  1. Read the file again to get current state
  2. Copy exact content including whitespace
  3. Use more surrounding context

Multiple Matches Found

Error: SEARCH block matches multiple locations Solution:
  • Add more context to make the match unique
  • Include function name, comments, or surrounding code
  • Use variable names or string literals as anchors

File Changed Externally

Error: File modified externally during operation Solution:
  • Refresh file content
  • Retry the operation
  • Use version control to resolve conflicts

Performance Considerations

Inline Editing is faster because:
  • Sends only SEARCH/REPLACE blocks
  • Doesn’t need full file content
  • Less token usage
  • Faster processing
Example:
  • Full file: 500 lines = ~15,000 tokens
  • Inline edit: 20 lines = ~600 tokens
  • 25x reduction in tokens

Configuration

Editor Settings

Configure your editor for optimal inline editing:

Ignore Formatting

For CLI environments with no auto-formatting:
See: src/core/prompts/system-prompt/components/editing_files.ts:84

Troubleshooting

  • Verify file isn’t read-only
  • Check file permissions
  • Ensure no syntax errors in REPLACE block
  • Confirm SEARCH block exactly matches file content
  • Add more context to SEARCH block
  • Include unique identifiers (comments, variable names)
  • Read file first to verify current state
  • Use function/class names as anchors
  • Disable auto-format temporarily
  • Match current file formatting in SEARCH blocks
  • Use the post-format content from tool responses
  • Configure consistent formatting rules

Next Steps

AI-Powered Coding

Learn how the AI decides when to use inline editing

File Identification

Discover files to edit with smart search

Auto Approve

Auto-approve safe inline edits for faster workflows

Checkpoints

Roll back inline edits if needed