Skip to contents

A tool that applies several exact-text replacements to one file, in order. Each edit follows the rules of tool_edit_file. If any edit fails, the file is left unchanged.

Usage

tool_multi_edit(path, edits)

Format

A tool definition created with ellmer::tool().

Arguments

path

Path to the file to edit.

edits

A list of edits, or a JSON array. Each edit has old_text, new_text and, optionally, replace_all.

Value

A status message with the number of edits and replacements.

Examples

path <- tempfile(fileext = ".txt")
writeLines(c("alpha", "beta"), path)
tool_multi_edit(
  path,
  list(list(old_text = "alpha", new_text = "gamma"))
)
#> [1] "Successfully applied 1 edit(s) to /tmp/RtmpZDl9Ug/file19f06827c02f.txt (1 total replacement)"
unlink(path)