Replace exact text in a file
replace_textReplace one exact text snippet in an Overleaf project file with new content, editing only that match and leaving the rest of the file untouched.
Instructions
Replace an exact snippet of text in a project file. Prefer it over write_file and edit_section for a targeted change: nothing outside the snippet is touched. findText must match exactly, whitespace and newlines included, and occur once unless replaceAll is set; replaceWith is inserted literally, so $$, $& and $1 stay as written. If the snippet is missing, or occurs several times without replaceAll, nothing changes and the reply gives the count; widen findText with surrounding text to make it unique. Pulls from Overleaf first, then edits only the local clone; push_changes publishes it. Returns how many occurrences were replaced.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | Path relative to the project root. | |
| project | No | Which project to use: its Overleaf address, its 24-character id, or a registered project name. Omit to use the default project. | |
| findText | Yes | Exact text to find, matched literally including whitespace and newlines. Not a regular expression. | |
| replaceAll | No | Replace every occurrence instead of requiring exactly one match (default false). Only set this when replacing all of them is intended. | |
| replaceWith | Yes | Text to put in its place. Empty string deletes the snippet. | |
| expectedRevision | No | Optional guard: the 64-character revision hash returned by read_file with mode=full or mode=smart for this same file. When given, the edit is refused if the file changed since that read — for example because pulling from Overleaf brought in a collaborator's version. Pass it whenever the edit depends on content you read earlier. |