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.
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:
Option | Description |
---|---|
yy | Copy the entire line. |
xyy | Copy the x number of lines. |
yaw | Copy a word with its trailing whitespace. |
yiw | Copy 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. |
ytx | Copy everything between the cursor and a specified character (x ) in the line. |
yfx | Copy 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:
To copy the entire second line, switch to normal mode, move the cursor to the beginning of that line, and press:
yy
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
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
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:
To do that, place the cursor at the beginning of the first line you want to cut from and pres 3dd
:
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:
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
Another option is to cut and paste lines in Vim. For example, cut the first three lines of the text with:
3dd
To paste the lines after the last line, move the cursor there and press:
p
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:
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
Next, move the cursor to the beginning of the text and press:
p
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.