replace - Replace Text
Overview
The replace tool replaces text within a file. By default, it replaces a single occurrence, but can replace multiple occurrences when expected_replacements is specified. This tool requires providing significant context around the change to ensure precise targeting.
Tool Name
- Internal Name:
replace - Display Name: Edit
- Icon: Pencil
Parameters
Required Parameters
| Parameter | Type | Description |
|---|---|---|
file_path | string | The absolute path to the file to modify. Must start with /. |
old_string | string | The exact literal text to replace (including all whitespace, indentation, newlines, and surrounding code etc.). |
new_string | string | The exact literal text to replace old_string with (also including all whitespace, indentation, newlines, and surrounding code etc.). Ensure the resulting code is correct and idiomatic. |
Optional Parameters
| Parameter | Type | Description |
|---|---|---|
expected_replacements | number | Number of replacements expected. Defaults to 1 if not specified. Use when you want to replace multiple occurrences. Minimum value is 1. |
modified_by_user | boolean | Whether the edit was modified manually by the user. Used for internal tracking. |
Features
Exact Matching:
- Must provide exact text match (including spaces, indentation, newlines)
- Do not escape
old_stringornew_string - Matching is case-sensitive
Context Requirements:
- For single replacements: Include at least 3 lines of context BEFORE and AFTER the target text
- Match whitespace and indentation precisely
- If string matches multiple locations or doesn't match exactly, the tool will fail
Multiple Replacements:
- Set
expected_replacementsto the number of occurrences to replace - Tool will replace ALL occurrences that match
old_stringexactly - Ensure the number of replacements matches your expectation
- Set
Security Restrictions:
- File path must be absolute
- File must be within project root directory
- Use
read_fileto examine file content before attempting replacement
Important Notes
If ANY of the following are not satisfied, the tool will fail:
file_pathMUST be an absolute pathold_stringMUST be the exact literal text to replace (including all whitespace, indentation, newlines, and surrounding code etc.)new_stringMUST be the exact literal text to replaceold_stringwith (also including all whitespace, indentation, newlines, and surrounding code etc.)- NEVER escape
old_stringornew_string
CRITICAL for old_string: Must uniquely identify the single instance to change. Include at least 3 lines of context BEFORE and AFTER the target text, matching whitespace and indentation precisely.
Usage Examples
Single Replacement (with context)
json
{
"file_path": "/home/user/project/src/app.ts",
"old_string": "function greet(name: string) {\n console.log('Hello, ' + name);\n return name;\n}",
"new_string": "function greet(name: string): string {\n console.log(`Hello, ${name}`);\n return name;\n}"
}Multiple Replacements
json
{
"file_path": "/home/user/project/src/utils.ts",
"old_string": "var ",
"new_string": "const ",
"expected_replacements": 5
}Replace Indented Code Block
json
{
"file_path": "/home/user/project/src/component.tsx",
"old_string": " render() {\n return (\n <div>Old Content</div>\n );\n }",
"new_string": " render() {\n return (\n <div>New Content</div>\n );\n }"
}Return Value
The tool returns an object containing:
llmContent: Operation result descriptionreturnDisplay: User-friendly display information (including file diff)summary: Operation summary (e.g., "Replaced 1 occurrence in file.ts")
Error Handling
Possible error scenarios:
Match Errors:
- Cannot find
old_string - Found multiple matches but
expected_replacementsis 1 - Actual replacement count doesn't match
expected_replacements
- Cannot find
Path Errors:
- Path is not absolute
- Path is outside project root directory
- File does not exist
Parameter Errors:
- Missing required parameters
old_stringornew_stringis emptyexpected_replacementsis less than 1
Workflow
- Read File: First use
read_fileto examine the file's current content - Plan Replacement: Determine the exact text to replace and context
- Execute Replacement: Call the
replacetool - Verify Result: Check if replacement was successful
Notes
- Always use read_file: Use the
read_filetool to examine the file's current content before attempting a text replacement - Provide Sufficient Context: For single replacements, include at least 3 lines of context BEFORE and AFTER the target text
- Do Not Escape: Do not escape
old_stringornew_string, that would break the exact literal text requirement - Verify Indentation: Ensure spaces and indentation match exactly
- Line Endings: Be aware of line ending consistency (LF vs CRLF)
Related Tools
read_file: Read file contentwrite_file: Write entire filesearch_file_content: Search file contentlist_directory: List directory contents
