wyc
wyc

Reputation: 55283

How can I replace a word all the files within a folder in Vim?

I know that by typing the following: :%s/iwanthis/replacedbythis/g will change all the matching words of the file. How can I do the same for all the files within a folder?

(actually replacing a lot of words like this: padding-bottom:5px;)

Upvotes: 7

Views: 1814

Answers (3)

balanv
balanv

Reputation: 10898

Hope this would be helpful for those who work without vim


find /your_path/to/folder -type f -exec sed -i 's/text_to_be_replaced/new_text/g' {} \;

This code replaces all the occurrences of the text in the specified path (/your_path/to/folder). Thought it might be helpful for someone.

Upvotes: 0

Vincent
Vincent

Reputation: 1141

you can try greplace.vim that can give you a buffer include all lines matching a given regex across multiple files, then you can modify things in the buffer, and then call another greplace command to make all the changes updated to all these files.

Upvotes: 0

Dave Kirby
Dave Kirby

Reputation: 26552

Open Vim with all the files loaded into buffers, and do the replace on all buffers at once with bufdo:

% vim *
... when vim has loaded:
:bufdo %s/iwanthis/replacedbythis/g | w

The | w will write each file back to disk.

Upvotes: 10

Related Questions