A command-line tool for finding and replacing text in files using regular expressions. This tool provides an intuitive interface for bulk text operations across your project files.
- Regular Expression Support: Use regex patterns for complex search and replace operations
- Glob Pattern Matching: Find files using glob expressions like
*.py,config.*, etc. - Recursive Search: Search through directory trees with the
-r/--recursiveflag - Interactive Confirmation: Review matches before making changes (unless using
--no-confirm) - Dry Run Mode: Preview what would be changed without making actual modifications
- Error Handling: Graceful handling of permission errors, encoding issues, and invalid regex patterns
- Cross-Platform: Works on Windows, macOS, and Linux
Install from PyPI:
pip install find-replace-cliReplace all instances of "old_function" with "new_function" in Python files:
find-and-replace "*.py" /path/to/project "old_function" "new_function"Replace text in all files recursively:
find-and-replace "*.txt" . "hello.*world" "hi universe" -rUpdate version strings in configuration files:
find-and-replace "config.json" ~/projects "\"version\":\s*\"[^\"]*\"" "\"version\": \"2.0.0\"" -rDry run to preview changes:
find-and-replace "*.py" . "old_pattern" "new_pattern" --dry-runNo confirmation prompts (automation-friendly):
find-and-replace "*.md" . "old_text" "new_text" -r -n- Code Refactoring: Rename functions, variables, or classes across your codebase
- Configuration Updates: Update configuration values across multiple files
- Documentation Maintenance: Update links, references, or terminology in documentation
- Migration Tasks: Update import statements, API calls, or deprecated syntax
- Bulk Text Processing: Any scenario requiring consistent text changes across multiple files
- Interactive Confirmation: By default, the tool shows you what will be changed and asks for confirmation
- Dry Run Mode: Test your patterns without making any changes
- Detailed Output: See exactly what files were processed and how many matches were found
- Error Recovery: Continues processing other files even if one file encounters an error
find-and-replace FILE_PATTERN DIRECTORY FIND_PATTERN REPLACE_TEXT [OPTIONS]
Positional Arguments:
FILE_PATTERN: Glob pattern for file names (e.g.,*.py,config.*)DIRECTORY: Directory to search inFIND_PATTERN: Regular expression pattern to findREPLACE_TEXT: Text to replace matches with (supports regex groups like\1,\2)
Options:
-r, --recursive: Search subdirectories recursively-n, --no-confirm: Skip confirmation prompts--dry-run: Show what would be changed without making changes-h, --help: Show help message
- Documentation: Read the full documentation at find-and-replace.readthedocs.io
- Issues: Report bugs or request features on GitHub Issues
- Source Code: View the source code on GitHub
This project is licensed under the MIT License - see the LICENSE file for details.
This document provides detailed information about the find-and-replace CLI tool's components and functions.
find-and-replace FILE_PATTERN DIRECTORY FIND_PATTERN REPLACE_TEXT [OPTIONS]
FILE_PATTERN
: Glob pattern for matching file names
: Type: str
: Examples: *.py, *.txt, config.*, package.json
: Description: Supports standard glob wildcards (*, ?, [...])
DIRECTORY
: Directory to search in
: Type: str
: Examples: ., /path/to/project, ~/documents
: Description: Can be absolute or relative path. Tilde (~) expansion is supported.
FIND_PATTERN
: Regular expression pattern to search for
: Type: str
: Examples: old_function, hello.*world, "version":\s*"[^"]*"
: Description: Full Python regex syntax supported. Remember to escape special characters in shell.
REPLACE_TEXT
: Replacement text (can include regex groups)
: Type: str
: Examples: new_function, hi universe, "version": "2.0.0"
: Description: Supports backreferences (\1, \2, etc.) for captured groups.
-r, --recursive
: Search subdirectories recursively
: Type: flag
: Default: False
: Description: When enabled, searches through all subdirectories.
-n, --no-confirm
: Skip confirmation prompts
: Type: flag
: Default: False
: Description: Useful for automation scripts. Applies changes without user interaction.
--dry-run
: Show what would be changed without making actual changes
: Type: flag
: Default: False
: Description: Safe way to test patterns before applying them.
-h, --help
: Show help message and exit
: Type: flag
hello: Matches literal text "hello"hello.*world: Matches "hello" followed by anything, then "world"\d+: Matches one or more digits\w+: Matches one or more word characters
"version":\s*"[^"]*": Matches JSON version fieldsimport\s+(\w+): Captures module names in import statementsfunction\s+(\w+)\s*\(: Captures function names
new_function: Simple text replacement\1_new: Prepend "new_" to captured group 1"version": "2.0.0": Replace with specific version
The tool handles various error conditions gracefully:
- File not found: Skips missing files with warning
- Permission denied: Reports permission errors and continues
- Unicode decode errors: Skips binary files with informative message
- Invalid regex: Validates patterns before processing
- Keyboard interrupt: Clean exit on Ctrl+C
- Files are processed sequentially
- Entire file content is loaded into memory
- Regex compilation is done once per pattern
- Large files (>100MB) may cause memory issues
- Test with
--dry-runbefore making actual changes - Use version control to track changes
- Escape shell metacharacters in patterns
- Start with simple patterns and build complexity gradually
- Use
--no-confirmonly for tested, automated scripts