Find and Replace all occurrences of a word with VIM

This post will be short one. If you have to work at remote servers with really slow internet connections and also have to edit files there you might get stuck with VIM. Yes sure, this program does not have a graphical user interface but it is extremely powerful! Do not underestimate it. One of the most common tasks for a text editor is finding and replacing a word or a entire pattern of course. Since VIM commands are a bit cryptic sometimes they are easy to forget if you do not use them on a daily base. The following one does replace one pattern by another one in the entire file. If your file is large this saves you a lot of typing!

[sourcecode]
%s/orignial pattern/new pattern/g
[/sourcecode]

Thats it. If you want to use special characters you have to put a “\” in front of them.

A space in vim can be replaced using “\s” or if it is more than one space “\s\s\s”

For tabulators you can use the pattern “\t”.

Leave a Reply