How to replace all occurrences in a text

By | January 19, 2014

One of the nasty tasks you sometimes have to do is to duplicate one file and replace a certain text (for example a subdomain name) with a different one (maybe another subdomain name?).

Sure, you can just go and edit it manually, you can download the file, use Find/Replace in an editor and I’m sure you can find even more ways.

However, you can do it much easier with vim:

vim file.conf

To replace

:%s/<original_text>/<new_text>/g

For example:

:%s/vps/demo/g

vim - replace all occurrences

Please note that the replace pattern can be a regular expression (which is quite cool!), so all special characters must be escaped.

For example, if you need to replace the whole subdomain name you will need to do something like this:

:%s/vps\.bubble\.ro/demo.bubble.ro/g

For more information on how to use search and replace with vim you can read this nice wiki page.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload the CAPTCHA.