Copy and Paste in Vim

A quick and easy guide on how to copy and paste using Vim

To copy and paste a chunk of code in Vim follow these instructions;

  • Place the cursor on the line you want to begin cutting.
  • Press V to select the entire line, or v to select from where your cursor is.
  • Move the cursor to the end of what you want to copy, using h,j,k, or l
  • Press y to copy it, or d to cut it.
  • Place the cursor where you would like to paste your copied stuff.
  • Press P to paste it before your cursor, or p to paste it after the cursor.

You can copy and paste between buffers.

Copying multiple lines in vim

If you want to copy multiple lines to put somewhere else, or you want to do things in “normal mode”, you can skip the v part from above and instead place the cursor on the line you want to begin cutting and then;

  • yy to copy the current line.
  • 5yy to copy five lines, starting from the cursor.
  • y$ to copy everything from the cursor to the end of the line.
  • y^ to copy everything from start of the line to the cursor.
  • yiw to copy the current word.

Once you’ve cut with one of these commands, you can press P or p to paste where you would like.

Copy and paste vim terminology

In this article we’ve used V, d, y, and P, amongst others.

  • V means “visual mode”, specifically a thing called linewise visual mode.
  • d means “delete” in Vim, which is kind of like cutting.
  • y means “yank” in Vim, which is kind of like copying.
  • P means “put” in Vim, which is kind of like pasting.

Why can’t I access the vim clipboard?

In other text editors, you will be used to copying using different keys, and when you do, the contents are available to paste in other applications.

That isn’t the case with vim, because we aren’t technically cutting as defined by the operating system. We are deleting the content and placing it into an internal register, accessible only to vim.

There are workarounds, but that is outside of the scope of this article!

And that is it! Now you can copy or cut and paste in vim!

Thanks to Paul for spotting a typo.


Recent posts View all

Web Dev

Creating draft posts in Jekyll

How to create and develop with draft posts in Jekyll

Ruby

Forcing a Rails database column to be not null

How you can force a table column to always have something in it with Rails