Cut and Paste in Vim

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

To cut 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 cut, using h,j,k, or l
  • Press d to cut it, or y to copy 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 cut and paste between buffers.

Cutting multiple lines in vim

If you want to cut 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;

  • dd to cut the current line.
  • 5dd to cut five lines, starting from the cursor.
  • d$ to cut everything from the cursor to the end of the line.
  • d^ to cut everything from start of the line to the cursor.
  • diw to cut the current word.

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

Cut 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 cutting 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 cut or copy and paste in vim!


Recent posts View all

Web DevMarketing

Getting more out of Plausible

Some things we've done to up our Plausible analytics game

Web Dev

Creating draft posts in Jekyll

How to create and develop with draft posts in Jekyll