VIM Fundamentals

Advanced Motions: Paste & Move

ThePrimeagen

ThePrimeagen

terminal
VIM Fundamentals

Check out a free preview of the full VIM Fundamentals course

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

ThePrimeagen answers a student's question regarding how to create a new line to paste on while in visual mode. More advanced movements including how to hold pasted code, utilize the system's clipboard, remapping in normal mode, and moving selected code snippets.

Preview
Close

Transcript from the "Advanced Motions: Paste & Move" Lesson

[00:00:00]
>> The question is, I just highlighted some small space using visual mode meaning not visual line mode visual mode. But when I pasted it paste on that line, I wanna be able to get a new line in and do that effectively doing an O for me, leaving insert and then pasting it in right?

[00:00:16]
So let's go and edit our vim RC. We can go down here, and we can actually just make up our own one right now. We can do, how about this one? Leader P. So I'm gonna do space P. And what it's gonna do is it's going to do O, so now I've done O, let me erase this thing.

[00:00:34]
I've done an O, I've created a new line at this point I'm in no remap mode right? And then I'm gonna do a paste and there we go. So I believe that should be it. It might be a shift pace. I think it's a shift pace. There we go.

[00:00:47]
So I'm going to take this and let's see what happens. Let's shout out percent sign. There we go. I'm going to highlight just this word. I'm gonna yank it, and I'm gonna go leader P. There we go, awesome, I already had something called with a different Pete lipsy.

[00:01:02]
We forgot to leave insert mode escape. There we go, shout out, remember I have an already a leader p map item. That's why there's that long wait, but let's just imagine that I don't so leader p. My goodness, what did I do? I forget the shout it out.

[00:01:24]
I copied that, that's why. [SOUND] All right there we go. So let's yank it, leader P. There we go. We just went and we just did that little bit off when you get the idea. So if I noticed that I enter, and a little bit down, I could follow it up with the j.

[00:01:38]
Shout it out, looks. Well that's not what i'm supposed to be doing. I must have done something wrong here. I copy that somewhere else? Supposed to be sourcing but anyways, so let's just source percent sign. So i should be able to leader P and now should paste it and i should be able to go up j is the wrong way, you get the idea though, right?

[00:01:57]
I meant to do k, here we go, di word, take that, shout it out. And leader p, there we go. I was able to do it and put myself back on that line. So you can kind of experiment around and play with this and create things you really like.

[00:02:13]
Now I do want to show you something. So we've, we've kind of put all these things together we have a bunch of knowledge. Let me share with you some of my favorite Remaps right now before we get into anything else because this is just like, we're in the advanced text editing section, and so I want to really add some awesome ones.

[00:02:29]
So this one I think is one of my favourite ones. Now you have some yanked text, but you don't want to lose it, right? You want to be able to paste it, replace some text, but you don't wanna lose your original yank. Now there's a couple ways we could go about this one, you could use your history to re paste back out or paste from your history and always keep using it or two, we could create a really awesome remap.

[00:03:00]
I'm always in favor of super awesome remaps [COUGH] so let's do it. So this what we're gonna do, we're gonna like this when we're in visual mode, no recursive execution map. We're then gonna go leader p, right? Elite hair P that's the English version doesn't work. We're gonna do liter P, and we're gonna do quotes underscore P.

[00:03:22]
Now what this R quote underscore d capital P. What this is gonna do is we're gonna delete our current selection and send it to the void register. The Void register is simply a death knell, right? It's gonna yank it to nowhere. And that's it, so let's shout out this and let's do foo bar bass.

[00:03:43]
So I'm gonna take foo, I'm gonna highlight it, and then I'm gonna, shit, I'm gonna highlight it, and then leader P, I've now replaced it. If I look at my registers, you'll see I still have foo. Whereas if I didn't do that I wouldn't have had it. So now I can do this again leader p I have now replaced it again without losing my original one.

[00:04:02]
That's pretty damn awesome. Those are one of those that are just like just the greatest things ever. There's one other one that I didn't show you, paragraphs. Remember how I told you about contiguous blocks of code jumping by paragraph? Well guess what DAP, Delete by Paragraph, right? Delete outsides of the paragraph boom, works tip also works, you can delete contiguous blocks of code like that.

[00:04:22]
So let's do another one of these really awesome ones. I hope you're ready. So in visual mode, leader y and let's do a plus sign right here, and do that. So what does this do? Well, you don't know what the plus register is. The plus register, if you have the right things installed, it doesn't always work on Mac, you have like, I think it's like vim-gtk or something like that and then you have to ports install or brew install or Asked as Tim Cook if you can instal it.

[00:04:52]
I don't know what Apple requires. I know it's pretty locked down. But once you get it on, it allows them to communicate with your system clipboard. I think Neovim has it by default maybe then has it by default at this point. It's been so long, I forget. What it does is, it will send it to your system clipboard.

[00:05:06]
So this is fantastic. So now, when I do this, if I highlight something and I go leader y. I now have yanked it but I've yanked it into my Ctrl+V. I now can paste into my system clipboard, which is fantastic. There is a mode to make it so that every single yank will go into your system clipboard but you don't always want that right?

[00:05:29]
Only sometimes do you want that I had that mode on for a little bit. But then I kept like losing stuff because I was like, I'm gonna copy this address over here, I'm going to put inside this doc, let me delete this. Crap I just wrote over and again.

[00:05:42]
So instead I like to control when I go into the system clipboard versus the vim clipboard. And so I found that just to be incredible. But wait, there's more. So let's do the exact same thing but in normal mode, so you're thinking, what does that do? I mean, what what could that do?

[00:05:58]
Well, let me show you. You'll see it down low, like right there. I'm gonna go space y? Now look at what it's actually typed for me. It typed the double quotes plus y. I am now ready to yank into my clipboard, but now I can do a motion.

[00:06:16]
How about ap let's highlight in yank that whole continuous paragraph into my clipboard, all right? So you can now remap it in normal mode to be able to grab these large sections of text and put them right into your clipboard. My goodness, this is the greatest thing and I'd be just remiss if I did not mention as born it's a buddy of ours on Twitch, he he turned me on to this.

[00:06:39]
This was the greatest thing ever. This my goodness, this is just I love this thing. This is fantastic. He also showed me one more. That was fantastic. He does capital Y, and then does let's see what is that he does GGG. So you can imagine what this thing does.

[00:06:57]
g g goes to the top right? G goes down to the bottom. Well if I go space y I just copied the whole file, right? And so it's like a quick way for me to build just grab the entire file into my system clipboard. So if I'm not, if I'm in then this is fantastic.

[00:07:13]
Obviously from outside of them I can use like x clip and pipe a file into it. Pretty simple, but if I'm in them, I can do the exact same thing without going back out to the command line. I find this to be very, very nice or having to do something like VG go down here, space Y, right?

[00:07:28]
I don't want to have to do all that. It's just a beautiful, beautiful thing. There's a couple other ones but I have to look them up. And so let's just look these ones up cuz they're very fantastic. So these two are incredible. I just always forget how to write them.

[00:07:43]
Effectively, remember when we highlight a region and you press colon? It puts these funny characters down right here. Those funny characters have real meanings. Single quote opener is the beginning of a highlighted region. In a single quote close is the end of a highlighting region. And so effectively what I'm gonna do is I'm gonna do this special command called a move, and I'm gonna move it to the outside of a highlighted region.

[00:08:11]
And then I'm gonna hit enter. Then I'm gonna highlight my previous highlight which is gv, very fantastic command, but it's really only used for Like plugins, so if I go like this, watch this. I highlight this region I leave and then I do gv, it will re highlight my previous highlight.

[00:08:26]
I'm gonna press equal sign, which will automatically indent or format my code, and then redo gv again, which will re highlight my section and what effectively does this do? Well, if I highlight this and do shift j, I can move code using capital K and capital J, that's pretty darn neat.

[00:08:46]
And if there's functions, this probably doesn't work, but if I do this, it does work, awesome. It will automatically indent them and put them right where I wanted them to be all right? This is pretty darn cool. All right, this is pretty darn legit. Man, you just gotta come up with these cool little remaps to just do a lot of pretty awesome stuff.

[00:09:06]
Those are all fantastic. They're just ways that I just make them a little bit better, right? These are things that are just super hard to express and other editors, but are super simple to do here and I just absolutely think that that thing is fantastic.

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