Lesson 3 of 26

Copying, Pasting and Yanking in Vim

Duration: 3:30 Watch on YouTube (external link, opens in new tab)

Markdown version

In this lesson

Roy explains that Vim calls copying yanking, done with y, and pastes with p after the cursor or Shift-P before it, whether the yank is a visual selection, several lines selected with Shift-V or a whole line. He shows gp, which leaves the cursor just after the pasted text, yanking a word with a motion (yank in word), copying the whole page with Shift-G, Shift-V, gg and y, and copying columns in Ctrl-V column selection mode. Registers are left for a separate video.

Transcript

559 words

[00:09] (external link, opens in new tab) Vim has a different name for copying. It's called yanking. So if you want to copy something, use the letter Y. For example, if I go into visual mode (external link, opens in new tab) with V and I select the current these words and I press yank, then what's going to happen now is that I have this thing in memory. And how do I paste it? I use P or shift P. So P will paste after the cur just after the current cursor. So I'm going to press P. And I see that I have pasted this.

[00:40] (external link, opens in new tab) I'm going to press U for undo. But if I press shift P, it inserts this P copy before the cursor. Going to undo that, too. And this works for anything that's selected. So, for example, I'm going to select line shift V. I'm select multiple lines. Press Yank and I'm going press shift P and it pasted those lines just above my current cursor as new lines.

[01:12] (external link, opens in new tab) If you want to copy a whole line, just press Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y and this copies the current line in its entirety. Then you can press shiftp right here. You can also press GP which will actually bring the cursor just after inserting the new text. So let's say that I want to yank the current word. Motions work here too. So I'm going to say

[01:43] (external link, opens in new tab) Yank in word. This actually copied the word darkness here. I'm going to go down here. I'm going to press GP, which pastes the word darkness and moves the cursor just after it. I'm going to undo it. I'm going to say G, shiftP. I basically get the same thing. It doesn't look like it matters because I had nothing before the cursor in this current line, but basically it will paste the word just before the cursor versus just after and move the cursor to the end of the pasted text, which in this case kind of looks the same.

[02:21] (external link, opens in new tab) So that's yanking and pasting. That's what you're going to do most of the time. For example, if you want to copy the whole page, you can press shift G to go to the bottom of the page, shift V to select the whole line and then GG to go up to the beginning of the page and press Y. And now you've pasted the whole page. You've you've copied the whole page. Now you can paste it anywhere with shift P or P just like this. And we've copied the whole thing.

[02:52] (external link, opens in new tab) You can of course go to into column selection mode with Ctrl +V. Select all these columns here and press Y. And now they're all pasted. They're all copied. If I go here and I press P, I should have the columns pasted right here. Let's see. I'm going to add more lines here just so we can see what it looks like. And you can see all the columns here.

[03:22] (external link, opens in new tab) So that's it for copying and pasting. We still haven't talked about registers, which is a really cool concept. I'm going to cover that in a separate video.