gemini-cli - 💡(How to fix) Fix Newline characters in python strings are not escaped by `write` and `replace` tools [1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
google-gemini/gemini-cli#26209Fetched 2026-04-30 06:44:30
View on GitHub
Comments
0
Participants
1
Timeline
1
Reactions
0
Author
Participants
Timeline (top)
labeled ×1

Error Message

Which causes a syntax error and confuses the hell out of Gemini.

Code Example

print("Hello\nworld")

---

print("Hello
world")

---

> /about
About Gemini CLI│                                                                                                                                                                                                                                                                                                                                           │
CLI Version                                                                                                        0.40.0Git Commit                                                                                                         3d5bdc052                                                                                                                                                                                                              │
Model                                                                                                              Auto (Gemini 2.5)Sandbox                                                                                                            no sandbox                                                                                                                                                                                                             │
OS                                                                                                                 linux                                                                                                                                                                                                                  │
Auth Method                                                                                                        Signed in with Google (nickcasavant@google.com)Tier                                                                                                               Gemini Code Assist EnterpriseGCP Project                                                                                                        x-alife-analysis                                                                                                                                                                                                       │
IDE Client                                                                                                         VS Code
RAW_BUFFERClick to expand / collapse

What happened?

I am using Gemini CLI to write python code that generates markdown text. The issue is that the write and replace tools treat new line characters in python strings as new lines in the new python text file. For example:

If this is the line of python the CLI is attempting to write to the file:

print("Hello\nworld")

This is how it actually renders using the write or replace commands:

print("Hello
world")

Which causes a syntax error and confuses the hell out of Gemini.

What did you expect to happen?

I expected print("Hello\nworld") to be written print("Hello\nworld")

Client information

<details> <summary>Client Information</summary>

Run gemini to enter the interactive CLI, then run the /about command.

> /about
│ About Gemini CLI                                                                                                                                                                                                                                                                                                                          │
│                                                                                                                                                                                                                                                                                                                                           │
│ CLI Version                                                                                                        0.40.0                                                                                                                                                                                                                 │
│ Git Commit                                                                                                         3d5bdc052                                                                                                                                                                                                              │
│ Model                                                                                                              Auto (Gemini 2.5)                                                                                                                                                                                                      │
│ Sandbox                                                                                                            no sandbox                                                                                                                                                                                                             │
│ OS                                                                                                                 linux                                                                                                                                                                                                                  │
│ Auth Method                                                                                                        Signed in with Google ([email protected])                                                                                                                                                                        │
│ Tier                                                                                                               Gemini Code Assist Enterprise                                                                                                                                                                                          │
│ GCP Project                                                                                                        x-alife-analysis                                                                                                                                                                                                       │
│ IDE Client                                                                                                         VS Code
</details>

Login information

No response

Anything else we need to know?

Seems related to https://github.com/google-gemini/gemini-cli/issues/22466

extent analysis

TL;DR

The issue can be fixed by properly escaping or handling newline characters in Python strings before writing them to a file using the Gemini CLI.

Guidance

  • Verify that the Gemini CLI is correctly interpreting the newline characters in the Python strings by checking the documentation for any specific handling or escaping requirements.
  • Check the related issue https://github.com/google-gemini/gemini-cli/issues/22466 for any potential workarounds or fixes.
  • Consider using a different method to write the Python code to the file, such as using a templating engine or a library that can handle newline characters correctly.
  • Test the write and replace commands with different types of newline characters (e.g. \n, \r\n, etc.) to see if the issue is specific to a particular type of newline character.

Example

# Example of how to escape newline characters in a Python string
print("Hello\\nworld")

Notes

The issue seems to be specific to the Gemini CLI and its handling of newline characters in Python strings. The related issue https://github.com/google-gemini/gemini-cli/issues/22466 may provide more information on the cause and potential fixes.

Recommendation

Apply workaround: The issue is likely due to the Gemini CLI's handling of newline characters, and a workaround such as escaping the newline characters or using a different method to write the Python code to the file may be necessary until a fix is available.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

gemini-cli - 💡(How to fix) Fix Newline characters in python strings are not escaped by `write` and `replace` tools [1 participants]