Substitute Across File0:37
We can type /, and let's say my designer has said they want to change from using gray to slate. So we could search for gray, and see that we've got quite a few occurrences of gray in this file. So if we want to replace this, we can type :%s, and in this case the % refers to the current file, and s is for substitution. We can then type a /, followed by the thing we want to search for, so in this case we're searching for gray. And then we can type another /, and enter our replacement. So let's say we want to replace it with slate.
And that's because by default this search and replace will only make one change per line, similar to a regular expression. If we want to make the change global to the line, then we can add a /g on the end, and now you'll notice it's caught this extra gray here. Once we press enter, the file has been changed, and you'll see it's jumped down and made 44 substitutions on 36 lines. If we jump back to the top, I want to show you another cool thing. So let's say our designer has told us they want to change every occurrence of font-semibold to font-medium. Well if we just do a search for font-semibold, we'll see down the bottom here we've got four
Confirming Replacements2:34
And once again we can add g to the end, and press enter. Let's come back up to the top, and now let's say our designer has told us that the text on the website is too small, and we want to change the occurrences of text-sm to text-lg, but perhaps not in every case. So let's do %s/text-sm/text-lg/gc, which will confirm. When we press enter now, it'll jump us between the matches, so the one here in grey is the current one it's asking to replace with. So I can say yes to replace this one, and no to not replace that one, and you'll see
Substitute Within Selection3:47
Well, you'll notice when I have this visual selection, if I press :, we get this weird set of characters down the bottom here. This first set of characters here is effectively the start of the visual selection, and then a comma, and then the end of the visual selection. We can then type out s, and then let's say link, and then we're going to replace this with a, and then we can press enter. So we'll see now, these ones have been replaced with a tags, but the ones out here have not. All right, let's say we want to do some searching that doesn't involve replacing. Let's say we want to find every line that doesn't end with a greater than symbol. Well, we could type :%g for global, and then a /.
Global Search With Regex4:20
Let's say we want to find every line that doesn't end with a greater than symbol. Well, we could type :set g for global, and then a slash. We could add a regular expression in here, and we'll negate the search for our greater than symbol, close that off, and then we'll put a $ to say that it has to be at the end of the line. And if we want to print them, we could say p, and we press enter, we'll get a list of every line in the file that does not contain a greater than symbol at the end of the line. Now something you might notice about what we typed here is we have a g, we have a regular expression, and then we have a p. So g, r, e, p, grep.
Project-Wide Search Replace6:11
Let's undo that one as well. Now let's say we want to do a find and replace across our entire project, not just this one file. The first thing we want to do is a global search. So let's do our space g to open our live grep in Telescope. And how about we search for MaxWidth7XL? So we've got a couple of matches here. We've got two occurrences in the authenticated layer and one in the dashboard. We can use the tab key to select individual items in here, and then we can press Alt Q to open them in our quick fix list, which we'll talk about in a second.
Saving and Reviewing Diff8:02
So let's do a cd, and we want to do a substitution and replace max-width: 7xl with max-width: 6xl. Let's run that substitution, and it'll go through and change all of those. You'll notice in the quick fix list, it still shows the 7xl because the quick fix list is showing the preview of what was loaded into it. But if we jump through the quick fix list, you'll see they've all been changed with 6xl. Now currently these files haven't been saved, which we can see by a little * at the top here. So let's say :w to save every file. And if we jump over to our terminal here and do a git diff, then you'll see all of our changes here.
