Skip to main content

Edit

Performs exact string replacements in files with atomic writes for safe editing.

Common Properties

  • Name - The custom name of the node.
  • Color - The custom color of the node.
  • Delay Before (sec) - Waits in seconds before executing the node.
  • Delay After (sec) - Waits in seconds after executing node.
  • Continue On Error - Automation will continue regardless of any error. The default value is false.

Inputs

  • File Path - string - Absolute path to the file to modify (required).
  • Old String - string - The exact text to find and replace (required).
  • New String - string - The text to replace it with, must be different from old_string (required).

Options

  • Replace All - bool - Replace all occurrences of old_string instead of just the first one (default: false).

Outputs

  • File Path - string - Path to the edited file.
  • Replacements - int - Number of replacements made.
  • Modified - bool - Whether the file was actually modified.

How It Works

The Edit node performs exact string replacements in files using atomic write operations. When executed, the node:

  1. Validates all required inputs (file path, old string, new string)
  2. Ensures old and new strings are different
  3. Verifies the file exists
  4. Reads the entire file content
  5. Performs replacement operation:
    • Replace All = false: Finds the first occurrence
      • Validates the string is unique (appears only once)
      • Replaces only that occurrence
    • Replace All = true: Finds all occurrences
      • Replaces every occurrence in the file
  6. Writes the modified content atomically:
    • Creates temporary file in same directory
    • Writes new content to temporary file
    • Preserves original file permissions
    • Atomically renames temporary file to target
  7. Returns the number of replacements made

Requirements

  • Valid absolute file path
  • File must exist
  • Old string and new string must be different
  • Write permissions on the file
  • For single replacement: old string must appear exactly once

Error Handling

The node will return specific errors in the following cases:

  • Missing file path - "File path is required"
  • Missing old string - "Old string is required"
  • Missing new string - "New string is required"
  • Strings are identical - "Old string and new string must be different"
  • Relative path - "File path must be absolute"
  • Directory traversal - "Path cannot contain '..' for security reasons"
  • File not found - "File not found: {{path}}"
  • Read error - "Failed to read file: {{error}}"
  • String not found - "String not found in file: {{string}}"
  • Multiple occurrences (when Replace All = false) - "String appears {{N}} times in file. Use replace_all=true or provide more context to make it unique"
  • Write error - "Failed to write file: {{error}}"

Usage Examples

Simple String Replacement

Replace a single occurrence:

File Path: /home/user/config.txt
Old String: database_host=localhost
New String: database_host=production.example.com
Replace All: false

Replace All Occurrences

Replace all instances of a string:

File Path: /home/user/script.js
Old String: var
New String: const
Replace All: true

Update Configuration Value

Change a specific configuration:

File Path: /etc/app/settings.conf
Old String: port=8080
New String: port=9000
Replace All: false

Refactor Code

Rename a function call throughout a file:

File Path: /home/user/src/utils.py
Old String: old_function_name
New String: new_function_name
Replace All: true

Fix Typos

Correct a spelling mistake:

File Path: /home/user/docs/README.md
Old String: recieve
New String: receive
Replace All: true

Usage Notes

  • The edit operation uses exact string matching (not regex)
  • Old string must be different from new string
  • When Replace All is false, the string must appear exactly once
  • If multiple occurrences exist, use Replace All or provide more context in the old string
  • Line endings are preserved exactly as they appear in the original file
  • The operation is atomic - the file is never in an incomplete state
  • Original file permissions are preserved
  • Whitespace and special characters must match exactly
  • For regex-based replacements, use a different approach or preprocess with Grep

Common Use Cases

Configuration Updates

Update configuration values in config files (JSON, YAML, INI, etc.).

Version Bumping

Update version strings in package files.

Code Refactoring

Rename variables, functions, or classes within a file.

Text Corrections

Fix typos or standardize terminology across documentation.

Path Updates

Update file paths or URLs in configuration files.

Template Customization

Replace placeholder values in template files.

Best Practices

  • For single replacements: Include enough context in old_string to make it unique
  • For multiple replacements: Use Replace All = true
  • For complex edits: Consider using Multi Edit for multiple changes in one operation
  • Test first: Verify the exact string to replace (case-sensitive, including whitespace)
  • Backup critical files: Before making automated edits
  • Use Read node first: To verify the current content before editing
  • Handle errors gracefully: Enable Continue On Error if the change is optional

Comparison with Other Nodes

  • Edit vs Write: Edit modifies existing files; Write overwrites entire files
  • Edit vs Multi Edit: Edit makes one type of change; Multi Edit makes multiple different changes
  • Edit vs Grep: Edit replaces text; Grep searches without modifying