Agent tools

multi_edit

multi_edit({ path, edits: [{ old_string, new_string, replace_all? }] })

Several exact replacements in one file, applied in order, all or nothing. One turn instead of one per change.

category: writein orderatomic
Using Python?

Switch any code block to Python: the choice applies to every page. Python SDK guide and resource reference.

pathstringrequired

The file.

editsarrayrequired

Each with old_string, new_string and optionally replace_all.

All or nothing

If one replacement cannot be made, none of them is written. A file is never left half-changed — which matters when the changes depend on each other, as a rename across a file does.

In order, and that is visible

Each edit sees the result of the one before

So an earlier replacement can create — or destroy — the text a later one is looking for. List them in the order they appear in the file and keep their contexts distinct.

{
  path: "src/auth.js",
  edits: [
    { old_string: "const TTL = 3600",     new_string: "const TTL = 900" },
    { old_string: "// TODO: rotate",      new_string: "// rotated on refresh" },
    { old_string: "oldName",              new_string: "newName", replace_all: true }
  ]
}
{
    "path": "src/auth.js",
    "edits": [
        {"old_string": "const TTL = 3600", "new_string": "const TTL = 900"},
        {"old_string": "// TODO: rotate", "new_string": "// rotated on refresh"},
        {"old_string": "oldName", "new_string": "newName", "replace_all": True},
    ],
}

When it is the right tool

A rename across one file, a set of related changes, applying what a grep found in a single file. Across several files it is one call each — there is no multi-file edit.