Lesson 10 of 26
Search and Replace with Regex
In this lesson
Roy explains that Vim's search and replace is based on regular expressions, and replaces every run of word characters in a file with three underscores, using backslash w for any alphanumeric character, a plus for one or more and the global flag. He points to the vimregex.com site as a reference for character classes and quantifiers. He then replaces all uppercase characters, and after that all runs of non-uppercase characters, with underscores.
Transcript
[00:15] (external link, opens in new tab) So, if you thought, you know, kind of basic search and replace was kind of cool, which if you did, you're very, very geeky and nerdy, which is awesome. I appreciate the fact that you're even watching a Vim class. That should definitely give you some props. So if you like that realize that all of the vim search and replace is based on regular expressions. So if you know regular expressions you can be very very powerful. So for example for a regular expression we don't have to have hard-coded values. We can say
[00:45] (external link, opens in new tab) In this file substitute anything in the current reax. So in this case, let's choose everything that is kind of a word character or alpha numeric. So you just put the special back slashw for any alpha numeric character. And of course you can put a slash plus to say it has to be one or more. And now we've finished the search regular expression. And let's replace it with anything that is underscore.
[01:21] (external link, opens in new tab) Let's say three underscores and we'll do this globally of course and if I press enter now we've replaced all of the word characters so it's a pretty powerful thing we have a bunch of regular expressions that you can use in in in Vim and there's a site called Vimregex vimregex.com that you can use to look up it's pretty nice and simple so for example all the whites characters all the digits hex digit etc etc so /w W is what I I really like to use. And of course all the quantifiers.
[01:52] (external link, opens in new tab) So zero or more, at least one or more, etc., etc. And these are like the most common ones that I'll usually use. Also note that you can also say uppercase or or lowerase or non-upercase by having this uppercase and non- lowerase by having this uppercase. So for example, if I want to replace all the uppercase characters in this file, I'll say in this file substitute everything that is
[02:24] (external link, opens in new tab) This would be uppercase but I want to say not uppercase. Well actually let's say uppercase in this case uppercase in this case. Oh I killed me. And replace that with these three underscores and do that globally. If I press enter, replace all of my uppercases. And now I'm going to undo and if I just replace that with an uppercase U, I have done the exact opposite of that. In fact, let me do this.
[02:56] (external link, opens in new tab) Replace it for non-upercase at least one or more replace that with three. So it's not per one or but basically for any sequence of non-uppercase characters. So that's an example of using regular expressions and counters in your Vim substitutions. It's pretty pretty powerful.