How to Copy, Cut, and Paste in Vim

October 3, 2024

Introduction

Vim is an open-source text editor installed by default on most Linux distributions. While working in Vim, copying, cutting, and pasting text are frequently used options.

In this tutorial, learn how to copy, cut, and paste in the Vim editor.

guide on how to copy and paste using Vim

Note: If you don't have Vim on your system, check out our guides on How to Install Vim on Ubuntu.

Copy, Cut, and Paste in Normal Mode

In normal mode, you can copy with the y command (yank), cut with d (delete), and paste with p (put). These commands work on characters, lines, or blocks of text, providing flexibility for manipulating content efficiently. To enter normal mode, press the Esc key.

The following text explains how to copy, cut, and paste text in Vim.

Copy (Yank) in Vim

Copying text in Vim is also referred to as yanking. Use the y key on the keyboard to perform this operation.

There are several yank command options. The following table elaborates on them:

OptionDescription

yy
Copy the entire line.

xyy
Copy the x number of lines.
yawCopy a word with its trailing whitespace.
yiwCopy a word without its trailing whitespace.
y$Copy everything on the right of the cursor to the end of the line.
y^Copy everything left of the cursor to the start of the line.
ytxCopy everything between the cursor and a specified character (x) in the line.
yfxCopy everything between the cursor and a specified character (x) in the line.

Once in normal mode, move the cursor to the needed place and use the appropriate command. For example, the following text has five lines:

Five lines in Vim

To copy the entire second line, switch to normal mode, move the cursor to the beginning of that line, and press:

yy
Copying line two in Vim

The output doesn't show any changes, but Vim does copy the second line.

You can also copy a single word in normal mode. For example, copy the word four. To do that, move the cursor to the beginning of the word and press:

yaw
Copy one word in Vim

Cut (Delete) in Vim

Cutting text is referred to as deleting in Vim. Use the d key when performing this operation.

The options for the delete command are similar to the ones for the yank command. For example, to cut the entire second line, move the cursor to that line and type:

dd
Delete the second line

The output shows the lines are deleted.

You can also cut lines, starting from the one where the cursor is located.

For example, cut lines three, four, and five:

Cut three lines in VIm

To do that, place the cursor at the beginning of the first line you want to cut from and pres 3dd:

Three lines deleted in Vim

Paste (Put) in Vim

Once you have selected text in Vim, whether it is using the yank or the delete command, you can paste it into the desired location.

In Vim, pasting is called putting, and the function is evoked with the p command.

You can paste (or put) text by moving the cursor to the wanted position and pressing:

p

This command pastes the selected text after the cursor.

To add text before the cursor, type the capitalized command instead:

P

For example, the following text has five lines:

Five lines of text on Vim

To copy the second line and paste it after the fifth line, go to the normal mode and move the cursor to the second line. Type:

yy

Move the cursor to the end of the fifth line and type:

p
Copy and paste a line in VIm

Another option is to cut and paste lines in Vim. For example, cut the first three lines of the text with:

3dd
Cut three lines to the top in Vim

To paste the lines after the last line, move the cursor there and press:

p
Paste three lines in Vim

Note: Refer to our tutorial on how to show lines in Vim to learn how to display absolute, relative, and hybrid line numbers.

Copy, Cut, and Paste in Visual Mode

Alternatively, copy and edit text using the visual selection feature. This mode allows you to select text by navigating through a file.

Use Esc to exit out of the previously used mode and enable visual selection by pressing:

  • v (lowercase) to start selecting individual characters.
  • V (uppercase) to select the entire line.
  • ctrl+v to select by block.

For example, use ctrl+v to select the following:

Select by block in Vim

After you have selected the wanted text, press:

  • y to yank (copy) the content.
  • d to delete (cut) the content.
  • p to put (paste) the content.

For example, cut the selected text with:

d
Three lines cut in Vim

Next, move the cursor to the beginning of the text and press:

p
Visual block in Vim paste lines

Once you have edited the text in Vim, save the file before you exit.

Conclusion

This article explained how to copy, cut, and paste in Vim using the normal mode or the visual mode.

Next, explore the many options Vim has to offer with our Vim commands cheat sheet.

Was this article helpful?
YesNo
Sara Zivanov
Sara Zivanov is a technical writer at phoenixNAP who is passionate about making high-tech concepts accessible to everyone. Her experience as a content writer and her background in Engineering and Project Management allows her to streamline complex processes and make them user-friendly through her content.
Next you should read
How to Install Vim 8.2 on CentOS 7
September 10, 2019

Official repositories struggle to keep pace with updates and new software versions. This article provides...
Read more
How to Change and Use Vim Color Schemes
July 11, 2024

Vim color schemes are a handy feature for setting syntax highlighting. You can choose from a wide variety of...
Read more
How to Show or Hide Line Numbers in Vim / Vi
February 28, 2022

You can activate line numbering in Vim by showing absolute, relative or hybrid lines. Displaying or hiding...
Read more
How to Undo and Redo Changes in Vim / Vi
October 10, 2024

Mastering basic Vim commands includes learning how to undo and redo changes in this text editor. Knowing how...
Read more