vim

How to Copy Paste in Vi

Vi/Vim is commonly used as a primary text editor. Although Vi and Vim are the same, Vim is also recognized as an improved version of Vi with code highlighting. For text files, copying, cutting, pasting, etc., are the text files’ popular operations. This article will discuss some of these special operations of text files and see how they are performed in Vi/Vim.

Now, we will explain multiple methods to copy and paste in Vi. We will also include copying and pasting in different modes.

Copy, Paste, and Cut in Normal Mode

The system automatically opens the normal mode when you open the Vim editor. Vi/Vim commands can be executed in this mode and navigate through files.

Copy Operation in Vi/Vim

We also know the copy operation as the yank operation under Vi/Vim. Here, we keep the cursor in the desired position while copying the text. Our next step would be to use the “y” command followed by the movement command. Some of the famous yank (copy) commands are as follows:

y% This command copies the text between similar characters, such as brackets ( ). For example, if there is any text between the brackets ( ), you can copy it with the help of this command.
yiw This command copies the current word.
yw This command copies the text where the cursor is placed at the beginning of another word.
y^ This command copies all text, starting at the beginning of the line where the cursor is positioned.
y$ This command copies all text, starting from the cursor’s position at the end of the line.
2yy This command copies the two lines, starting with the cursor line.
yy This command copies the line where we placed the cursor, and the newline character is present.

Paste Operation in Vi/Vim

In Vi/Vim, the paste operation is called a put operation. The only way to paste in Vi/Vim is to place the cursor at the desired location and use “P” to paste text before or after the cursor.

Cut Operation in Vi/Vim

In Vi/Vim, we know the cut operation as the delete operation. In order to cut the text, it is best to place the cursor in the desired position. After this, you can use the ‌movement commands using the “d” command. Some of its famous delete (cut) commands are as follows:

d% This command cuts the text between similar characters, such as brackets ( ). For example, if there is any text between the brackets ( ), you can cut it with the help of this command.
diw To cut the current word.
dw This command cuts the text where the cursor is placed at the beginning of another word.
d^ Using this command, you can cut all text starting from the cursor’s position at the beginning of a line.
d$ This command cuts all text, starting from the cursor’s position at the end of the line.
2dd This command cuts the two lines, starting with the cursor line.
dd This command cuts the line where we placed the cursor, and the newline character is present.

Copy, Paste, and Cut in Visual Mode

You are entering ‌the visual mode shown as an initial selection point. You can also manipulate and select text in Vi/Vim’s visual mode.

  • First, place the cursor on the line you want to cut or copy the text.
  • There are three types of visual modes, and they are:
Press “v” For accessing the visual mode.
Press “V” Select the text by line by using the visual line mode.
Press “CTRL” and “V” For switching to the visual block mode.

This mode allows you to select text by using rectangular blocks.

Click on the text you wish to cut or copy. Here, you can move left, right, down, and up using the arrow keys.

  • To cut or copy a selection, press “d” or “p”, respectively
  • You could paste the text more easily if you moved the cursor to where you want to paste it.
  • To paste, simply press “p” after the cursor or just after it.

Conclusion

We hope you understand how to copy, paste, and cut in Vim. Sometimes, you may need to make configuration changes to your package; copy-pasting or cut-pasting the text in Vi/Vim can do this. We hope you found this article helpful. Check the other Linux Hint articles for more tips and tutorials.

About the author

Prateek Jangid

A passionate Linux user for personal and professional reasons, always exploring what is new in the world of Linux and sharing with my readers.