Skip to content

Instantly share code, notes, and snippets.

@jhult
Forked from jefflombard/ag_replace.sh
Last active June 30, 2023 03:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhult/c3ddb9db38a2f73ccfccbf4186c75b39 to your computer and use it in GitHub Desktop.
Save jhult/c3ddb9db38a2f73ccfccbf4186c75b39 to your computer and use it in GitHub Desktop.
rgr command - Find and Replace with rg on macOS
#!/bin/zsh
rgr() {
# find and replace
regex="s?${1}?${2}?g;"
rg $1 -l | xargs sed -i.rgr_backup $regex
# delete backups
find . -name "*.rgr_backup" -print0 | xargs rm
}
# Original based on ag (The Silver Searcher): https://gist.github.com/jefflombard/c016d3a929ebf5dd1da5bcaddc64e72a
# This code is based on rg (ripgrep): https://gist.github.com/jhult/c3ddb9db38a2f73ccfccbf4186c75b39
# Usage on macOS
# 1. Install rg - https://github.com/BurntSushi/ripgrep (`brew install ripgrep`)
# 2. Copy lines 3 - 9 into your .zshrc
# 3. Go to the target directory and call with "rgr"
# Example
# Replace "test" with "hello": "rgr test hello"
@jhult
Copy link
Author

jhult commented Oct 17, 2021

@ElectricRCAircraftGuy
Copy link

FYI for anyone: I wrote a really robust ripgrep wrapper that allows full replacement via -R. See rgr.sh here: https://github.com/ElectricRCAircraftGuy/ripgrep_replace

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment