-
Notifications
You must be signed in to change notification settings - Fork 0
Quick Start Tutorial
Get up and running with sr-search-replace in minutes. This guide focuses on the tool's core strength: simplified search and replace syntax with minimal parameters.
- Core Syntax
- Basic Commands (1 Minute)
- Simple Replacements (5 Minutes)
- Working with Multiple Files (10 Minutes)
- Essential Features (15 Minutes)
- Safe Search & Replace Workflow
- Common Use Cases
- Troubleshooting
The simplified sr-search-replace syntax:
sr "<pattern>" "<replacement>" [path]With file pattern matching:
sr "*.[extension]" "<find>" "<replace>"Examples:
-
sr "*.txt" "old" "new"- Replace in all .txt files -
sr "*.js" "const " "let "- Change variable declarations -
sr "*.html" "<div>" "<section>"- Update HTML structure
These are the most essential sr-search-replace operations:
Replace text in a single file:
sr "old_text" "new_text" /path/to/file.txtReplace across all files with specific extension:
sr "*.py" "old_name" "new_name"Recover from a previous operation using automatic backups:
sr --restore /path/to/file.txtThis reverts the file to its state before the last replacement.
Display all available options:
sr --helpFind and replace in all files in current directory:
sr "TODO" "DONE" *.mdBy default, replacement is case-sensitive:
sr "Error" "Warning" *.logThis matches only "Error", not "error" or "ERROR".
Use glob patterns to target multiple extensions:
sr "deprecated_function" "new_function" *.{js,ts,jsx,tsx}Search entire directory tree:
sr "*.js" "console.log" "console.debug" --recursive ./src/This processes all .js files in ./src/ and all subdirectories.
Skip certain directories (minified, compiled, etc.):
sr "*.js" "old" "new" --exclude "node_modules" --exclude ".git" --recursive .Always create backups of modified files:
sr "*.java" "oldAPI" "newAPI" --backup --recursive /project/Backups are saved with .bak extension.
See what will be changed before applying:
sr "*.sql" "UPDATE" "INSERT" --dry-run database/No files are modified when using --dry-run.
Use regex for complex pattern matching:
sr --regex "function\\s+(\\w+)" "const $1 =" *.jsMatches function declarations and converts them to const assignments.
Ignore case when matching:
sr --ignore-case "api" "API" *.mdThis matches "api", "API", "Api", and replaces with "API".
View match count without making changes:
sr --count-only "TODO" *.jsUseful for auditing before replacement.
Display surrounding lines:
sr --context 3 "password" *.pyShows 3 lines before and after each match.
Track related replacements as a session:
sr "*.java" "OldClass" "NewClass" --session "refactor_v2.0" --backup
sr "*.xml" "oldProperty" "newProperty" --session "refactor_v2.0" --backupAll changes grouped under "refactor_v2.0" session.
Automatically skip binary files:
sr "*.txt" "old" "new" --skip-binary --recursive .Prevents corruption of image/compiled files.
Recommended workflow for production environments:
First, test what will match:
sr --count-only "old_pattern" *.txtConfirm number of matches.
See exact modifications without applying:
sr "old_pattern" "new_pattern" --dry-run --context 2 *.txtCreate backup before applying (automatic with --backup):
sr "old_pattern" "new_pattern" --backup --session "update_v1.2" *.txtCheck that changes were applied correctly:
sr --count-only "new_pattern" *.txtCount should match previous results.
If needed, restore from backup:
sr --restore *.txtChange module imports across project:
sr "*.py" "from old_module" "from new_module" --recursive .Rename classes with backup:
sr "*.java" "class OldName" "class NewName" --backup --recursive src/Correct mistakes in markdown files:
sr "*.md" "occassion" "occasion" --recursive docs/Change settings across config files:
sr "*.conf" "timeout=300" "timeout=600" --backup /etc/Clean up deprecated imports:
sr "*.js" "import {oldFunc}" "// import {oldFunc}" --recursive src/Update function calls:
sr "--regex" "oldAPI\\.call\\(" "newAPI.execute(" *.ts --backup --recursiveTry these steps:
- Use --count-only to verify matches exist:
sr --count-only "your_pattern" target_file- Check for case sensitivity:
sr --ignore-case "your_pattern" "replacement" target_file- Enable regex if using special characters:
sr --regex "your\\.pattern" "replacement" target_file- Preview first with --dry-run:
sr "pattern" "replacement" --dry-run --context 2 files- Restore if needed:
sr --restore affected_file- For large directories, be selective:
sr "*.txt" "old" "new" --recursive data/ # Target specific directory- Skip unnecessary file types:
sr "*.py" "old" "new" --skip-binary --exclude "*.pyc" --recursive .- Use session tracking for complex operations:
sr "*.js" "old" "new" --session "update" --backupNow that you understand the basics:
- See Command Reference for all available options
- Learn advanced patterns in Regular Expressions
- Read Pattern Matching Guide for complex searches
- Check Basic Examples for more use cases