Skip to content

Instantly share code, notes, and snippets.

@johngraham262
Last active August 29, 2015 13:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johngraham262/8846509 to your computer and use it in GitHub Desktop.
Save johngraham262/8846509 to your computer and use it in GitHub Desktop.
Git grep is a great search too, but I often find myself wanting to git grep for a term and then *replace* it too. With the "gsr" command, you can type in a search_term and a replace_term and it will search-->replace all matches. It's case-sensitive too.
# gsr = Git Search Replace
gsr() {
search_string=$1
replace_string=$2
if [ -z $search_string ] || [ -z $replace_string ]
then
echo "-- Git Search & Replace (gsr)"
echo "-- usage: gsr search_string replace_string"
else
git grep -l $search_string | xargs sed -i '' "s/$search_string/$replace_string/g"
echo "Crushed it."
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment