How to search and replace text in Vim

Posted by

Here’s how to search and replace in vim (also known as “substituting” text).

Let’s say I want to replace the text “this” with the text “that”.  In vim, we can do:

This is saying, from line 1, to the end of the file (represented by $), replace “this” with “that”.  The “g” on the end is to do this multiple times on one line of text if applicable.
So you can mix this up and do it from line 1 to line 10:

Or from line 100 to the end:

Or only the first instance on each line (remove the “g”)

There you go.  Happy Vim substituting!

Leave a Reply

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