Skip to content

Instantly share code, notes, and snippets.

@victormier
Last active December 29, 2015 09:39
Show Gist options
  • Save victormier/7651313 to your computer and use it in GitHub Desktop.
Save victormier/7651313 to your computer and use it in GitHub Desktop.
Simple git grep replace for bash written in ruby. Replaces all instances of a string in any tracked file. Based on https://gist.github.com/jcamenisch/1671995 Allows strings with whitespaces.
#!/usr/bin/env ruby
if ARGV.length != 2
puts "Usage:"
puts ' gg_replace term replacement'
puts
puts 'Example:'
puts ' gg_replace "bad cappuchino" "fine cappuccino"'
exit
end
find = ARGV[0]
replace = ARGV[1]
files = `git grep -l '#{find}'`.split("\n")
files.each do |file|
`sed -e 's/#{find}/#{replace}/g' -i '' #{file}`
end
@victormier
Copy link
Author

Reminder: scape forward slashes

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