Created
January 24, 2012 19:19
-
-
Save jcamenisch/1671995 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
Here's my version: https://gist.github.com/glyph/9beafa8a7b26e5ca9f6666448fa5810d
A few notes:
GLOBIGNORE
~~~~~
in path namesgit grep
prefix all its output with./
sed
processes quite so many times