Search and Replace using the vi Editor
March 16th, 2008If you’re like me then you’re using the unix vi editor like an oxygen tank on mars. But seriously, I’ve always liked the search and replace feature from other graphical based text editors so it’s only fair that we learn how to use vi’s search and replace function. It’s extremely useful and easy once you get the hang of it.
:%s/old_string/new_string/g
Will replace all occurrences of “old_string” with “new_string” in your file. The “%” tells us to execute the search command over the entire file. The “g” tells us to search AND replace everywhere in the file. You could also replace a string across a range of lines, for example:
:32,56s/old_string/new_string/g
Will replace “old_string” with “new_string” from line 32 through line 56, pretty handy ![]()




Leave a Reply