Why does gemini-cli's replace tool fail with "could not find the string to replace" or "No changes detected"?
The gemini-cli replace (edit) tool often fails with "Error: Failed to edit, could not find the string to replace.", "0 occurrences found for old_string.", or "No changes detected." Users report it can repeat in a loop and leave the agent unable to make changes. What can be done about it?
Originally asked on GitHub: https://github.com/google-gemini/gemini-cli/issues/1028
6 answers
The issue was closed as a duplicate, and the thread does not show a released fix. A commenter said they are marking similar reports as duplicates of issue #4137, and asked to keep the topic open. A contributor's prototype (#1061) explores a different way of providing files to the model, but it is behind main and conflicts with it.
Workarounds mentioned in the thread:
- Fall back to
write_file. The reporter read the file, changed the content, and wrote the whole file back. A commenter warns thatwrite_filehas an internal limit and can truncate files, which can make things worse. - Ask Gemini to describe the changes instead of applying them. A user in duplicate issue #1635 wrote that they tell gemini-cli the latest edit ended with "No changes detected" and ask it to list the modifications so they can apply them by hand. The thread does not say whether this resolved the loop.
- Ask Gemini to use
sedfor the same replacement. A user says this is a temporary workaround. - Split long
old_stringvalues into smaller pieces. A user says this usually did not help.
Another commenter noted that after an edit changes the file on disk, the replace tool can keep failing because the original string no longer matches. Another suspected that pressing Escape during a change leaves stale state, but this is only a theory.
Did this work for you?- Fall back to
What a GitHub user tried on GitHub (Partly worked)
The reporter hit the replace error and switched to write_file: read the file, modify the content in memory, then write the new content to the file.
Source: https://github.com/google-gemini/gemini-cli/issues/1028 (opens external site)
Did this work for you?What @jackwotherspoon tried on GitHub (Outcome unclear)
A user in duplicate issue #1635 suggested asking gemini-cli to list its latest modifications so they could be applied manually, after an edit reported "No changes detected." The thread does not report whether this worked.
Did this work for you?What @Manamama tried on GitHub (Partly worked)
The user asked Gemini to perform the same replacement with sed instead of the replace tool, and sometimes had it write diffs to a new .md file for manual insertion.
Did this work for you?What @Manamama tried on GitHub (Didn't work)
The user asked Gemini to chunk long strings to be replaced into smaller pieces, down to sentence level.
Did this work for you?What @Manamama tried on GitHub (Worked)
The user showed Gemini the actual file state using grep and cat, and used a fork related to modelcontextprotocol/servers issue #2332.
Did this work for you?
Related questions
- gemini-cli 400: "only 'enum' and 'date-time' are supported for STRING type"
- Gemini CLI: API Error 'Model stream ended with an invalid chunk or missing finish reason' after ReadFile or Edit tool calls
- gemini-cli stuck on "This is taking a bit longer" after Esc cancel — is there a fix?
- Is there a way to get codebase indexing in gemini-cli?
- How can I use voice input instead of typing with Gemini CLI?