VIM Fundamentals

Delete, Yank, Post, & Visual Mode

ThePrimeagen

ThePrimeagen

Netflix
VIM Fundamentals

Check out a free preview of the full VIM Fundamentals course

The "Delete, Yank, Post, & Visual Mode" 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 delete a line, undo the last action, yank a line, paste, and the register is briefly discussed. Using visual mode to highlight code and use combinations of commands is also covered in this segment.

Preview
Close

Transcript from the "Delete, Yank, Post, & Visual Mode" Lesson

[00:00:00]
>> Now, I'm gonna curl thins down, it's the exact same thing again, mystery script from the internet, save it onto your computer, lets edit it, right? Fantastic, all right, it's of the exact same style as the previous one, except for now we're gonna learn some more commands. I know your brian's probably feeling okay, how many of these do we need to learn?

[00:00:18]
Don't worry, there's just a couple hundred we'll get through it in a jiff! All right, anyway, so let's press j to go all the way down to the bottom. All the way down to the delete, I mean. Right here, so we're at our first one. I'm gonna center it again by using zz, and to delete a line, just press dd.

[00:00:35]
Awesome, right? So, let's do that again. dd, dd, dd. Now I actually don't even use dd To tell you the truth. I use something a little different to delete lines. So press u to undo your last action. Yeah u for undo, hopefully this is fairly pneumonic. It feels pretty easy.

[00:00:52]
So let's go right back to that, so what I actually like to do, is I like to do, shift vd, why? Cuz I can do it super fast. I don't know, I just love it, it feels fantastic. I like doing that but you just choose what you want to do, and what really works with you.

[00:01:08]
Right, so now we're gonna talk about yanking, all right? Yanking is also a term that probably makes no sense, and if you're from Australia, I think that's what you call Americans. I've heard rumors that were called the yank somewhere, but either way, or maybe that's somebody else but either way yanking simply means to copy it to a location.

[00:01:28]
Now a yank is not like copying and pasting meaning it will not go to your system clipboard unless if you tell it to. So I'm gonna go like this yy. Now notice something, when I press that first y, down here in the corner, you can actually see the motion being started.

[00:01:47]
So if I were to type something else, it would actually keep that string of characters getting longer. So when I press it again, I've officially yanked. Now we're gonna paste it. So said yanking and paste below yy, and then p. p means paste, paste from this area that we've saved it to.

[00:02:04]
So when I press p, notice that it pastes it but one line down below and my cursor has moved down below. So let's do it again to the next one, press j to go down, and let's yank it again. But this time, let's yank it and press Shift p.

[00:02:19]
I'm going to play us yy and then shift p, look at what happened. I know this doesn't feel that revolutionary, but trust me, the more you use vim, the more you're gonna appreciate the difference between p and capital P. I pasted it, but the line that I was on, got shifted downwards.

[00:02:38]
And what I copied was put on that line instead. This is actually incredible I know, [LAUGH] It doesn't feel incredible, but I'm just telling you. This is incredible I know Aah! Anyway, so just experience it, enjoy it. Alright, so let's go down to the next part, visual mode.

[00:02:55]
I've mentioned it a few times this is shift and moving around and being able to highlight, right? So I'm gonna zz again to put it here. This line simply just says press v, so move somewhere into the line and just press v. At this point, once you've done that, you'll notice right away that down low it says visual.

[00:03:16]
This means that you're in visual mode. Now let's press w, Let's press j. Notice something about it, it highlights from where you started to where your cursor is. So as I move around, it just keeps highlighting. Interesting, right? Alright, so you can press Esc to stop. Or you can press Ctrl+c or control open bracket, you can press any of those and it will take you out of visual mode.

[00:03:42]
Awesome, so now let's do the next one. The next one down here says, let's use visual line mode. And so visual line mode is slightly different than visual mode. Visual line mode allows you to highlight a whole line at once. So when I press shift+v, it will highlight everything by the way, I'm still using just simply j to move downward.

[00:04:02]
So everyone you should be using j to move downwards. Notice that when I use j now, it just highlights the whole line, right? So now let's combine some things. Okay, so now we're gonna get a little bit rowdy. I hope everyone's feeling ready here. So let's go down, all right, press zz, re put ourselves into the middle.

[00:04:21]
And now let's highlight this line with shift+v and then press y. All right, here we go shift+v, the line is highlighted. Now I'm gonna press y, it stopped being highlighted. Why, what happened?
>> Just this copy, so that we can then paste.
>> That's right. You yanked it, so your visual region.

[00:04:44]
So now let's press p. Look what happened. We just yanked that line using capital V than why we're now combining two things into one. This is where you should start seeing, okay? There's an expressive nature to these motions and commands, that actually you can kind of combo them together.

[00:05:03]
It's kind of Mortal Kombat, just without fighting video games or Gore. So its effectively the exact same thing. So let's go down low, let's move somewhere in the middle of this. Let's press v, move forward again with w, and press y. Now you don't have to select the exact same region I did, but just select something.

[00:05:24]
All right, so I'm gonna like this, y. So if I press paste now, what do you think's gonna happen? Do you think it's gonna move my content down, and then put in a new line with that content? Well, I'd say you're right, except for you're wrong. So because I didn't select a full line, it knows that I'm only selecting a small non line region.

[00:05:48]
And so using visual mode causes both something that's incredible and really annoying sometimes. So you kinda have to know how to use it and what happens. So there's a little bit of state you'll have to keep in your head. But for the most part that happens, you can press u to undo it, awesome.

[00:06:03]
All right, so let's do the exact same thing, let's do with d, I kinda showed this earlier, you can shift+v and do d, and it will take it. Now watch what happens when I press y though. It actually just pasted what I deleted. So in vim when you delete, you also yank.

[00:06:24]
Now this can be very, very exciting. Okay, I don't know about you, but this is where the magic starts happening. Okay, we're almost there. We're getting there. And so that means as you delete, you can move code instead of having to be like okay the cop you, I need to now delete you, I now need to move you somewhere else.

[00:06:41]
Now I need to paste you, instead you just go delete, paste. So let's do the same thing but let's press d. Notice when I use visual mode, not visual line mode, but visual mode. I delete that contents within the line. And then I press p and p will read paste it back in, but it pasted back in one thing over.

[00:07:00]
And this is because of how the cursor position works and what happened. So if I were to undo that and press with capital P, notice that the line is exactly back to the way it was. It actually paste into the correct position, we deleted it, we put it back in.

[00:07:15]
Now, I don't often use visual mode, visual line mode I use from time to time. Visual mode is fairly often. There's only a couple reasons why use it. All right, so let's talk about the relationship between yanking and deleting. So, you at some point in the future can type in help read but for now, just as I had been holding ireg for command register.

[00:07:41]
when you do that, look at what we see. You see a lot of things right? But remember something that at one point, we did do a lot of deleting right? We did some deleting of the lines. I also deleted some other stuff, I did a vd on that whole highlighted line.

[00:08:00]
Notice that we have our delete history, in here for us automatically. And also notice that the last thing that I yanked or deleted is always at the top. So we're gonna get into registers later, but for now, I want you to know they exist. And they automatically keep track of the things you delete and the things you yank.

[00:08:22]
Now if I were to, let's do a little exercise and kinda see how they change stuff. So try to keep what you did in your head or look at my screen and try to keep that in you head what I've done, and let's do a couple little exercises, okay?

[00:08:34]
So press escape, to leave that command, and let's go down here and let's yank this line. So we'll do a yy. Now, let's relook at the registers :reg. Notice that only two lines have changed. The one that starts with a double quotes, which is up here at the top, so double quotes double quotes and the other one double quotes, zero.

[00:09:01]
Those are the only two that changed. So let's try something. Let's do a little bit of playing around. Let's close this down. And let's paste this line. Let's work all the way over to the line, press I, and let's just type in something, just doesn't really matter. Just type in whatever, press Esc, Ctrl+C or Ctrl open bracket to leave it, and then let's re yy it.

[00:09:26]
Now, we know that registers keep track of history, right? So let's relook at the registers. Let's see what happened. Okay, so it changed, but probably not in the way you were expecting, you're well, hey, it should have been keeping track of history. Why didn't it? So instead of telling you the answer, let's explore just a little bit more.

[00:09:47]
So let's leave this you can use Ctrl+C, you can use Esc, you can use whatever. So let's delete this line instead. Now let's look at the registers. So I'm gonna press colon, and then you can just press up to get your last command. Or you can type in reg to get it also has a little bit of like fuzzing on it.

[00:10:06]
So if you just type an r and then press up, it can also give you other commands that you've typed, with the same meaning. So I'm just gonna do :reg. Now let's look at it. Okay, so we do see that line I just deleted, but now we see it three times.

[00:10:21]
And all the other content got shifted down. In other words, yanks will be preserved for only one register your zeroth register, whereas deletes will be kept in order in history all the way down to the ninth one. We were able to kinda deduce what happened as opposed to reading the manual, and I love not reading the manual.

[00:10:40]
So this is fantastic. We don't even have to learn anything by reading. We just kinda discovered how it works. This is awesome. Right? So how often do I use this stuff? Virtually never, and I can show why later, but for now I don't use the kinda those implicit movements of the registers ever.

[00:10:57]
And one fun little fact, when you press p, what you're actually doing is copying the contents from the double quotes register or your implicit register and putting it into your programme. So that's kind of what is happening. All right, so let's leave that, awesome. Now we're gonna quit, classic conundrum you press :q, and it says, my goodness, you can't do that.

[00:11:19]
So this time, I'm gonna write quit it because it just feels right to do that at least once. All right, so I know this has been for those that are familiar with him. This might be a little bit dry, but you might have just learned something about registers.

[00:11:33]
So I'm gonna keep on doing this style of learning, where I'm gonna keep on calling into a more complex feature, and try to bring it in slowly. So that way, you know it exists. You may not know what a register is still, you may have no idea why it's happening, but you at least know two things.

[00:11:49]
You can yank and paste, it you can delete and paste it, and there is a list of your history somewhere. You may not know remember the command perfectly fine. So let's go to insert. Now you're probably thinking insert, it's just I right? No, there's a lot of ways to go into insert mode.

[00:12:08]
And it's quite fantastic. In fact, this is actually one of my favorite things about vim, is how many different ways you can go into insert mode. I know, this just a sounding ridic, as I say it out loud, I'm I really saying this out loud? Yes, I am.

[00:12:21]
I am saying this out loud. Either way, anyone, any questions, or is this pretty straight forward still?
>> We were grabbing snippets with visual not visual line mode.
>> Yeah.
>> And do you know any cool shortcuts to drop that onto the next line without, new line, Esc+paste?

[00:12:37]
>> No. As of right now I don't, but, we can make one. How about this one, when I start doing remaps, let's do it together. Sounds good? Awesome, we'll make something that makes it work. Cuz yes, that's actually a really great point is, I don't like that, how can I change Vim to operate the way I want it to be?

[00:12:56]
I've said this from the beginning, I'll keep saying it. Vim is your editor, your way. It has some defaults, but there's very few and the default is ugly. Let's make it pretty for us. Effectively, I was asked, if I highlight with v not capital V. So just going into visual mode, not visual line mode, and I yank a portion, is there a way we can make it?

[00:13:17]
So when I press p, it doesn't paste in that line, but instead creates a new line and then paste the content. The answer is both yes and no. We can make them do things we want. But the default behavior does not allow for that. And so we'll get there.

[00:13:34]
We'll do that here soon. He is responsible for trying to remind me to remember to do that. We'll see if we can work together and make this happen. You also sound a little bit more advanced. So, hopefully this intro stuff isn't too boring. Alright, so let's move on to the next part.

[00:13:48]
Here we go.

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