Last Updated: February 25, 2016
· davidann

Removing text from all files in a directory using Vim

When I'm working on a feature in Ruby/Rspec project, I'll commonly add :focus symbols to my specs.

This is great until I'm ready to merge in and I've forgotten where I put all the :focus symbols.

I hate the thought of going through each test file by hand and doing a search/replace, so I dug on the interwebs to see if there's a way to programmatically remove text from all files in a directory.

Luckily, there is.:)

  1. Open vim
  2. :args parent/directory/**/*.rb. This recursively opens .rb all files under parent/directory.
  3. :bufdo %s/find_this/replace_with_this/g |w. This does a search/replace on each open file, then saves it.

I found this at stackoverflow

1 Response
Add your response

Another alternative:

sed -i 's/find_this/replace_with_this/g' parent/directory/*/.rb

over 1 year ago ·