VIM Fundamentals

Search & Replace

ThePrimeagen

ThePrimeagen

terminal
VIM Fundamentals

Check out a free preview of the full VIM Fundamentals course

The "Search & Replace" Lesson is part of the full, VIM Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

ThePrimeagen demonstrates how to search in vim, navigate, highlight, and replace the results with regex. How to find and save the search results as variable is also covered in this segment.

Preview
Close

Transcript from the "Search & Replace" Lesson

[00:00:00]
>> I hope everyone enjoyed that, is there any questions? Was that kind of intense, probably a lot of information there, does do. Does everyone see the power of quick fix list at least, right? Do you see why they're so awesome? What you can do with them. See, do, looks really cool, hopefully you saw that one ha.

[00:00:16]
I wonder what I with them, I didn't really go over that because it's not, probably worth, it's not the right time yet anyways. We haven't kinda gone over a lot of the commands, but I just personally really liked that. So let's keep on going, let's do search and replace, all right?

[00:00:31]
Search and replace, allows you to be able to do exactly what it sounds like. And it's something that we just, you kind of like need for every editor at this point. It's kind of crazy not to have it, right. It just doesn't feel like an editor. It was in head, head had search and replace.

[00:00:45]
So you bet you, this has search and replace. So we have another one of these curl files. I'm going to take it, I'm going to cut and paste it and I'm going to put it into the chat right now. Please download it, let's do this, let's do some searching and some replacing.

[00:00:58]
Now at this point, we're getting into kind of a lot of these more advanced areas, right? So this is kind of where VIM, we're really kind of diving into more of the vim isms, things that don't necessarily exist in all editors. Quick Fix list obviously don't really exist in all editors.

[00:01:15]
But there we go, so let's start with the searching. Now if you use Neo-vim versus vim, you may have different results. Since I use Neo-vim it may act differently than how your search and replace will work. So let's find out, I believe I should be able to like this set_nohls, there we go.

[00:01:31]
That should be, I believe this should be the, that should be how vim should work. So if you're using vim, these now should line up approximately. So let's start by searching for the word error. How do you search in vim? While you type in a slash, so if I go slash, I'm now searching in this document.

[00:01:52]
As you can see, it's a forward slash. And so I'm gonna type in error, there we go. Unfortunately, I still have incremental search on, it's going to show me what the first match that I find. There we go, I have found it. If I press n, it will jump to the next one, that's, there we go.

[00:02:08]
See, as you can see, I'm pressing n and it's jumping around, it'll eventually let me know that I've hit the top. And there we go, you can do capital N, to walk backwards. Alright, awesome, so now we're just searching. We can now set some commands. Let's talk about some different commands you can set, going to set hls and then also do set ic.

[00:02:28]
So I'm going to put them both in a single set command. What this will do, is this will set any highlighted searches that we've done. So my last search is highlighted right now. As you can see, it's just sitting, we searched up air, there's all the spots that it existed.

[00:02:42]
If I start to retype error, you'll see that it highlights it everywhere. Here's all the places re exists. Here's all the places that exists. Here's error, and you can see it's slowly going down and doing it. You can do more than just that type of searching, you can add in regex is now here's the thing with regex is you got to always go to regex licensing.org.

[00:03:07]
You're gonna look on here and make sure you have, you're gonna see that there's a lot of typical disasters with regex is be careful with them. And you're gonna check the citations and make sure you haven't been cited for any illegal uses of regex, is don't put them into production kids, terrible idea, awesome.

[00:03:21]
And by the way, I am on the warning list. I'm on the No Fly regex list, so I'm kind of doing something a little out of my bounds right now. Please do not tell guy that I've done this. I will be in big trouble, awesome, all right. So I'm going to dot star, look at it.

[00:03:34]
It's matching everything, right, then we do Or, you can see that it's matching across. So incremental search is incredible, because it shows you, how your search is matching up in that file. Highlight search or hls, not a big fan personally, because after I've searched, I don't need all the things highlighted.

[00:03:52]
And if I want them to go away, I have to manually type in nohls highlight search or have a remap. I haven't done a remap, I could do one, I could use this. If I wanted to do that every single time, not necessarily something I want to do, but it is available.

[00:04:08]
All right, so, let's do something a little bit different. You can go and you can highlight a region with V, do a visual mode. And then, you can type in a colon. It will give you a ranged search. Now you can do a search and replace on this.

[00:04:24]
You can just do a search on this. I can do a sweet error search, across only that visual region. So here, let's tighten it up a little bit. Here, let me undo that, I'm gonna just do these two websites. I must have under, my goodness, must have done that one wrong.

[00:04:40]
So let's just do this and let's just make it a little bit more interesting. Honestly, let's just make it more interesting. Let's search over a region, but let's do a search and replace. What can we do with the search and replace? Well, how it works is it just works like it doesn't head, you do a search with s slash the token you're looking for slash the token, you're going to replace it.

[00:05:01]
So we can do the exact same thing here, I can search for error, and then I can replace it with something else, if you're doing a full file search, you do percent s. If you're doing it over a visual range, you first select that visual range, and then do this and then do s and it can do it over a visual range.

[00:05:17]
So there we go, I can start typing. Here's the three places that have found it. I can replace it with AOEU, which is my equivalent of your asdf. And there we go, a replaced it but notice something. It only replaced one of them in this line. It's pretty interesting, right?

[00:05:32]
So let's go down here, I kind of have some stuff right in here to be able to do that. We're going to do it, let's do it right here. Let's go all the way down to the foot section and do the same thing. But let's do it, best, all right, I'm gonna, I cannot sit on this line and type it s slash foo.

[00:05:49]
And you'll see it highlights all undo bar, right or bars, knows that only does it one line at a time. So I can redo up, do it over and over again. And notice how I'm executing that too. If I do s slash, it's on a single line, percent S, for the whole file, or if you have a visual range selected, it is tick opening bracket, tick closing bracket, right?

[00:06:10]
And that's a visual range, all right, but if we wanna be able to replace them all, and at the same time, we can get to that line. And we could do foo bar slash g, awesome, it'll go globally across the line replace them. This should be pretty familiar if you've ever used any regex is, this should feel pretty familiar.

[00:06:30]
At this point, you kinda know what that means. We can go down even further and do it again. But this time let's do a s slash foo bar gc, I showed you this earlier. You can press a yes for yes replace it or no, don't replace it. So, I can go yes, no, yes, no, there we go, I was able to find replace.

[00:06:51]
Yes, no, yes, no, so you do have a lot of options when you're using the searching and the replacing, it's pretty fun. Let me show you a little bit the range one down here. So let's say we want to do all the foos, right here. Well, I could just highlight them, right?

[00:07:07]
And then do s/foo/bar, it only did it over my highlight. I didn't have to worry about the other foos in there. That's pretty nice, because you don't want to necessarily always be searching or placing the entire file, or just constrained to a single line. You want to be able to kinda select a region, right?

[00:07:26]
We've already talked about the full file, it's awesome. So full file, finding replace or full project, I kind of alluded to it in quick fix, you can use grepping across quick fixes and then use a C do, to be able to execute certain, finding replaces on it globally.

[00:07:44]
So it acts very similar and then a write all to be able to write all of them, awesome. So that's effectively finding replace. Works with regex is, but let's add in something that makes it a little bit more fun. Let's like this, if let's have a, term in here, let's just call it foo for now.

[00:08:01]
I'm gonna take off highlighted searches. So okay, we're kind of off the script right now. Let's just deal with it, right, and what I want to do is, I want to be able to find and replace this. But let's go man, you know what we did wrong. We meant to find everything that, we meant to search for falses right, not trues but falses, I'm gonna have to manually go in here and type in, not foo, not bar, not bass.

[00:08:27]
Obviously, if this was a long list, you'd first question the person's programming talent. But, instead we're just simply going to go, okay, well let's find and replace this and do this better, right? So one thing we could do, well, we could try to go all right. Well, there is this where I could go, If space this and then all right, we can do that and we can replace it.

[00:08:52]
Oops, I forgot to do it s slash, alright, we could replace it with if that bang, awesome, okay, that works, right? We did something kind of cool there, we kind of replace something in there but, that's not as good, right? We could do something better, let's try to do something a little bit better and a little bit more confusing.

[00:09:12]
Alright, so let's do this again except for let's take foo and bar or bar and bass, and bass and foo and swap them. So we can do this with fine, we can do this with finding replace and regex. So we're going to do this, I'll highlight this region.

[00:09:28]
So it's just right here, right now can find, my search space. And what I'm going to do, is I'm going to first look for a opening princely alright, we got it. I call this the one I Kirby, you're gonna go find norm, effectively you're escaping a open parentheses then you're going to do dot star.

[00:09:47]
Then you're going to do a closing parentheses and then I'm going to do a space ampere ampere. Space, another one I Kirby, and then I'm going to do another parentheses right here, so there we go. So what I've effectively done, is I've matched a group of characters, but I've put this escaped parentheses around them.

[00:10:07]
What that tells my regex to do, is that not only should we find that contiguous space of characters. But also, I want to save it to a variable. So now I'm going to do a slash. And what I'll do is I'll go okay, so let's see we had a parentheses.

[00:10:22]
I'm gonna do slash two and slash one, right? So slash two is the second match group, slash one is the first match group. So should go, bar foo, it should be able to effectively swap, right? So let's hit enter, there we go. We kind of got it, but my goodness, look what happened.

[00:10:39]
We totally got this wrong, so what happened here? Let's start playing with it, let's see if we can kind of figure out what went wrong. So we're gonna go back to this and we're gonna press up, all right. So let's start moving it back, right, let's start walking it back.

[00:10:53]
So notice what's happening as I'm writing this, if I just do this, you'll notice it highlights the whole region. Okay, well that's bad, so maybe I had myself some bad escaped items. Now, because this seems to be working, right. Why is it that dot star slash this, why is it that this is not matching?

[00:11:14]
So you can type it in you see it, and then maybe try something else. What is one, okay, awesome, I must have done something goofy there deny. So, let's try it again, oops, I just replaced it twice. All right, let's try slash two, know that also worked. I bet you, that now that I've kind of did a little bit of diagnostic thing, that the ampersand sign must have a meaning I don't understand, look at that, it hasn't meaning I don't understand.

[00:11:41]
Okay, so if I wanted to replace this, I'm simply gonna do slash one space slash ampere slash ampere. You kind of have to experience experiment with this slash two. And I bet you this will actually work. Don't forget to, see I forgot to wrap it with, the rest of the stuff that we matched in the previous one.

[00:11:58]
But as you see, I was able to flip them, by typing them correctly by doing two and one there we go. I flipped them around, right, I know I had to do the other ampersand science plus the squirrely brace. We just didn't do it, but you get the idea.

[00:12:12]
I'm able to kind of work with it, see what happens, redo it over and over again and you can see how you can find and replace with regexes. Also, this is a great example of why regexes are likely more confusing than you think they are. They often don't act the way you think they are.

[00:12:25]
You think you understand what they're matching, and sometimes you're wrong. You're over matching, you're under matching or you're executing some unknown variable that actually does something right. Classic, \is the classic conundrum, right?

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now