Last Updated: February 25, 2016
·
415
· coldgrnd

search & replace in matching files

reoccurring task:

  • replace every occurence of FOO with BAR
  • only in *.h and *.cpp files

    ag -G "\.(h|cpp)" FOO -l | xargs sed -i 's/FOO/BAR/g'

In this command I use ag -- the silversearcher

  • ag -G allows you to narrow the search to certain file types
  • -l will cause ag to only list files that contain the pattern