Copy, Cut, and Paste In Vim / Vi

One of the most used operations when working with text files is copy, cut, and paste operations. Vim provides a lot of different and detailed features about copy, cut, and paste operations by using commands and key shortcuts. These copy, cut, and paste operations are related to each other and generally used in a row. In this tutorial, we learn how to copy, cut, and paste in Vim/Vi by using commands and key shortcuts.

Copy (Yanking)

The copy operation will copy the specified part of the given file content. There are different commands to copy different parts of the content. In order to copy commands first the normal or command mode should be enabled with the ESC key. So please press the ESC key before the following copy commands.

  • yy – Copy the current line where the cursor is located with the newline character.
  • 5yy – Copy 5 lines down from the current line where the cursor is located with the newline character.
  • y$ – Copy all lines down from the current line where the cursor is located to the end of file.
  • y^ – Copy all lines up from the current line where the cursor is located to the start of the file.
  • yw – Copy from the current location where the cursor is located to the next word start. T
  • yiw – Copy the current word which the cursor is located from start of the word to the end of the word.
Copy with yy and Paste with p

Cut (Deleting)

The cut operation will cut the specified part of the content and put it into the buffer in order to be used later. The cut operation is also called delete where the specified content will be removed from the current context. In general, the d key is used to cut or delete specified content and the part of the content we want to cut will be specified with some specifiers explained below.

  • dd – Delete/Cut the current line where the cursor is located.
  • 5dd – Delete/Cut all 5 lines down from the current location where the cursor is located.
  • d$ – Delete/Cut from the current line where the cursor is located to the end of the file.
  • d^ – Delete/Cut from the current line where the cursor is located to the start of the file.
  • dw – Delete/Cut the current word.
See also  How To "Select All" In Vim/Vi?

Paste (Putting)

The paste will put the current buffer content into the cursor position. The buffer can contain multiple items but the current buffer content is the latest cut or deleted content. Like previous commands and keys first press the ESC key.

  • p – Paste/Put the recent buffer content into the cursor location.
  • P – Paste/Put the recent buffer content before the cursor location.

Leave a Comment