Search & Replace with Ripgrep
Intermediatev1.0.0
Use ripgrep for codebase-wide search and replace — previewing changes, piping to sed for replacement, using capture groups, and safely refactoring across large projects.
Content
Overview
Ripgrep excels at finding patterns, and paired with sed or other tools, enables safe codebase-wide refactoring. Preview changes before applying, use capture groups for smart replacements, and verify results.
Why This Matters
- -Refactoring — rename functions, variables, and APIs across files
- -Migration — update import paths, API endpoints, and config keys
- -Cleanup — remove debug statements, fix typos, update patterns
- -Safety — preview all changes before committing
How It Works
Step 1: Preview Matches
Step 2: Simple Replacement with sed
Step 3: Regex Replacement with Capture Groups
Step 4: Safe Refactoring Workflow
Best Practices
- -Always preview with rg before applying sed
- -Use -l to get file list (pipe to xargs sed for replacement)
- -Create a git branch before bulk replacements
- -Verify with rg after replacement (no remaining old references)
- -Run tests after refactoring
- -Use -w for whole-word replacement (avoid partial matches)
Common Mistakes
- -Replacing without previewing (unintended changes)
- -Not using -w (replacing partial words: "user" -> "new_user" in "username")
- -Forgetting to escape regex in sed (dots, slashes)
- -Not verifying after replacement (broken references missed)
- -Not running tests (replacement broke functionality)
- -Using BSD sed syntax on Linux or vice versa
FAQ
Discussion
Loading comments...