Skip to main content
Racing Turtles

Vim (Cut, Copy, Paste) - Use the Clipboard to Edit Text

Learn how to manipulate the clipboard to cut, copy, and paste text with the clipboard.




Introduction #

The last tutorial detailed ways to enter insert mode in Vim. You learned how to enter Insert mode before or after the cursor, at the beginning or end of the line, and in a line created above or below the current line. You can enter insert mode, but not cut, copy, or paste text.

This tutorial describes how to cut, copy, and paste text. You will learn how to cut, copy, and paste a line of text.

Requirements #

Lack Requirements?

If you lack the requirements, check the first tutorial for instructions on how to install and get started using Vim.

Getting Started #

Open Vim. For detailed instructions on how to open Vim, read the "Getting Started" section of the previous tutorial.

Insert Text #

Insert some text into the buffer. Press i to enter Insert mode and enter the following text:

There is some strange text in this file.

Clipboard #

When you cut or copy text, Vim stores that text in a register. The text will remain in that register until you clear the register or put new text in the register. Vim stores the text in a default register if no register is specified. You can specify a register to operate on with the " key. For more information on registers, visit [the copying section][vim-help-cp-m] of the Vim documentation.

Cut Text #

To cut text until a specified character, enter d followed by a command to move the cursor to that character. For example, to cut all the text until before the next t, use the command dtt where tt is the command used to move the cursor to the character directly before the next t.

Place the cursor at the beginning of the file with the command gg0. Move the cursor to the beginning of the word strange with the command 3w. For more on cursor movements, check out this section of the Vim documentation.

Cut the word strange with the command dw. The text in the file should look like this:

There is some text in this file.

The cursor should highlight the first t in text.

Paste Text #

Paste the word strange before the word file.

Move the cursor to the character at the beginning of the word file with the command $B.

To paste the word strange before the word file, type P. The P command inserts the text stored in the current register before the cursor. The text in the file should look like this:

There is some text in this strange file.

Copy Text #

To copy text, enter y followed by a command to move the cursor to the end of the text you want to copy. To copy the current highlighted line, enter yy.

Line Edits

To perform a clipboard operation (and many other operations) in Vim, enter the letter for the command twice. For example, to cut the current line, enter dd.

Copy the first line of the file with yy. The text shouldn't change.

Paste Text Again #

Paste the copied text below the current line with p.

The p command pastes text in the register after the cursor, and the P command pastes the text after the cursor. The text should look like this:

There is some text in this strange file.
There is some text in this strange file.

Conclusion #

In this tutorial, you learned how to cut, copy, and paste text in Vim. You learned how to load a register with text. Vim provides clipboard functionality to let you cut, copy, and paste text anywhere you want.

License #

This work is licensed under CC BY 4.0.