-
-
Save christilden/3317308 to your computer and use it in GitHub Desktop.
Lightning-fast project-wide find/replace with git grep and sed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gg_replace() { | |
if [[ "$#" == "0" ]]; then | |
echo 'Usage:' | |
echo ' gg_replace term replacement file_mask' | |
echo | |
echo 'Example:' | |
echo ' gg_replace cappuchino cappuccino *.html' | |
echo | |
else | |
find=$1; shift | |
replace=$1; shift | |
ORIG_GLOBIGNORE=$GLOBIGNORE | |
GLOBIGNORE=*.* | |
if [[ "$#" = "0" ]]; then | |
set -- ' ' $@ | |
fi | |
while [[ "$#" -gt "0" ]]; do | |
for file in `git grep -l "$find" -- $1`; do | |
sed -e "s/$find/$replace/g" -i'' $file | |
done | |
shift | |
done | |
GLOBIGNORE=$ORIG_GLOBIGNORE | |
fi | |
} | |
gg_dasherize() { | |
gg_replace $1 `echo $1 | sed -e 's/_/-/g'` $2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Adds " characters on line 21 in order to support find/replace of terms with spaces. For example, gg_replace "an cappuchino" "a cappuccino" *.html