Skip to contents

A tool that replaces exact text in an existing file. Unless replace_all is TRUE, old_text must appear exactly once. Line endings and the rest of the file are left as they were.

Usage

tool_edit_file(path, old_text, new_text, replace_all = FALSE)

Format

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

Arguments

path

Path to the file to edit.

old_text

Text to replace.

new_text

Replacement text.

replace_all

If TRUE, replace every occurrence of old_text.

Value

A status message with the number of replacements.

Examples

path <- tempfile(fileext = ".txt")
writeLines("alpha", path)
tool_edit_file(path, "alpha", "beta")
#> [1] "Successfully edited /tmp/RtmpZDl9Ug/file19f079671711.txt (1 replacement)"
unlink(path)