Skip to content

Instantly share code, notes, and snippets.

@christilden
Forked from jcamenisch/.profile fragment
Created August 10, 2012 19:44
Show Gist options
  • Save christilden/3317308 to your computer and use it in GitHub Desktop.
Save christilden/3317308 to your computer and use it in GitHub Desktop.
Lightning-fast project-wide find/replace with git grep and sed
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
}
@christilden
Copy link
Author

Adds " characters on line 21 in order to support find/replace of terms with spaces. For example, gg_replace "an cappuchino" "a cappuccino" *.html

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