Introduction to Bash, VIM & Regex Deleting and Searching
Transcript from the "Deleting and Searching" Lesson
>> James Halliday: Okay, so now we can start to combine some of these ways of hopping around with operators. So if we want to delete a character, you can just type x from command mode. So if I do x, x, x, x, x, x, x, x, but say, what if I wanna delete the whole line?
[00:00:21] Well, I can use the d operator for that, so there's a couple of ways of using the d operator. So I can do d$, which deletes from the current position to the end of line. So maybe if I'm not at the beginning of the line, so If I'm right here, I can do d$.
[00:00:42] Deletes everything to the end of the line. If I do d0 or D caret, it deletes everything from the current position to the beginning of the line, like so. Also there's if you need to undo something you can hit u in command mode. If you wanna redo something, you can do Ctrl + R, [INAUDIBLE].
>> James Halliday: Okay, so more fun things that we can delete. We can sort of combine the ways of skipping around even more, so for example, if we do dj, j is a way of going down one line. So what vim will do is it will delete from wherever you are to the next line and also delete that line.
[00:01:46] So this is a way of kind of deleting those two lines. Or you could also do dk, which deletes the current line and the next line. Or you can do dgg, which deletes from the current position to the beginning of the file. Or you can do dG, which deletes from the current position to the end of the file.
[00:02:11] So everything you just learned about moving around, you can also use to delete to different spots.
>> James Halliday: There's also some ones. I don't use these but maybe I should where you can do things like combine numbers. So if you do 5dd, it's gonna delete the next five lines, I think, yeah.
[00:02:34] Or if you wanna go back the other way, I think maybe d negative five, would that work? No. Anyways, lots of fun ways of combining these pieces.
>> James Halliday: But yeah, you can also dl, which deletes to the left and dh, which deletes to the right. If you don't wanna use x for some reason.
>> James Halliday: So you can also use regular expressions to hop around in them. So you could search for a pattern. For example, if we wanna, so if I do /cool, I'm gonna skip ahead to the next occurrence of the word cool. If there are multiple occurrences like here, I'll create a few more.
>> James Halliday: So if I search for cool with /cool from command mode, you can hit the N key, which goes to the next match. This is also true in less, which is the pager that you're in if you type get log or some other commands. You can use capital N to go back to the previous match, like that, so n, capital N.
[00:04:01] If you wanna search backward, instead of a forward slash, you can use a question mark. So from command mode, you can type a question mark and then do something like a pattern. Maybe question mark change will match change. The pattern system is uses regular expressions. So if you wanna search for maybe I'll put some numbers in this text file.
[00:04:30] So if you wanna search for some numbers, we can do that. So I'll put forward slash and then do a character class match for 0 through 9. And there I can skip through those results.
>> James Halliday: And the same goes for searching backward with question mark. So here is another fun thing that we can do, combining the matching searching capabilities with regular expressions with the delete operator.
[00:05:01] We could do things, I can delete from the current character by typing d and then I can do /
[0-9], with square brackets. And what this is gonna do is it's gonna, starting at the current position, it's gonna delete everything until it gets to a number. So if I do that, I just deleted everything up to this number not including the number.
[00:05:29] I'll show you again, so you do d/ and then the pattern that you want to delete
>> Speaker 2: And it deletes everything until it hits the pattern?
>> James Halliday: Yes.
>> Speaker 2: From where your cursor is.
>> James Halliday: Yeah, and not including the pattern. There's also, you can now do dn as well, which deletes to the next occurrence of the pattern.
[00:05:58] So if you already had a pattern that you'd just done, you could do dn, which deletes to the next occurence. Or dN, which searches from the current position to the previous occurrence.
>> Speaker 2: After you do that, you delete something, is there a way to get out of that mode?
[00:06:20] Cuz I still see my pattern down there after I deleted to that pattern.
>> James Halliday: Do you, let me see here.
>> James Halliday: You still see the pattern after you hit Enter or.
>> Speaker 2: Let me try again.
>> James Halliday: Okay.
>> Speaker 2: Keep going.
>> James Halliday: Okay [LAUGH].
>> James Halliday: So there's some more ways to jump around, other than regular expressions, you can also use these operators f and t and also capital F and capital T.
[00:06:58] So for example, if I wanna skip, suppose because I have a sentence like this. If I wanna jump from my current position to that first period, I could go l, l, l, l, l, l, l, that's a lot of typing. What I can do instead is type f.
>> James Halliday: Which is a little bit easier sometimes than popping into Regex mode, especially with the dot, because dot is a meta character, so all I have to do /\.. So f dot to go to pattern, I'll set t dot to go to the character before that character that you're searching for.
[00:07:39] So do f dot, it goes straight to that character, if I do t dot, right before it. That works for any character, so if I wanna hop to the t, I can do t, capital T, right there. You can combine the t and f way of hopping around with deletes of course, as well.
[00:08:04] So if I wanna delete everything before the capital T, I could do dt capital T, and it deletes everything that precedes the capital T. So dT, capital T there, deletes everything before the T, not including the T. I could also do df capital T and that deletes the capital T, as well as everything preceding it.
[00:08:34] There's also if you capitalize d and t, you do a search backwards instead of forwards, so if I do a capital FI then it search backwards on the current line. I should mention again t and f only search the current line only Regex is which search the entire file.