Finding and Replacing Items in Vim
This guide examines the process for finding and replacing items inside of Vim by leveraging the %s command and passing it regular expressions.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

So in the last guide, we talked about how we can search and navigate through our search results in Vim. And in this guide, I want to talk about how we can do a kind of similar task, which is to find and replace items in a file. So, I have a good example right here of a class called MyClass, and then a instantiation of MyClass right here.

Now, you can also imagine, let's say that we had a few more of these. If we decided to change the name of my class, this could be a very annoying process to come and chain all of these items, so the way that you can fix this is to actually perform a find and replace to be able to remove all those items.

And the syntax for this is going to be where I start with a command and then pass in %s, so %s for search, then you pass in using a /, and this is going to be a regular expression. You pass in whatever you are wanting to replace, so this is the pattern we're wanting Vim to find, and then you pass in whatever you want to replace it with.

So, I'm going to change it to my AwesomeClass, and if I hit Return, you can see that that went through and it updated each one of these items, and that is all that you have to do in order to implement a find and replace inside of Vim.

%s/MyClass/AwesomeClass