vim

Vim Copy Paste

There are many situations when you have to copy certain data or text from one location in your files, i.e., code files, text files, and paste it to some other location. You can also copy and paste text and codes from any file while working within the Vim Editor. For this purpose, you will be using the yank “y” and the put, i.e., “p” command in the Vim editor to perform copy and paste. So, let’s just start this article by opening the shell console of the Ubuntu 20.04 system using Ctrl+Alt+T. After opening the shell terminal, you have to check if vim is installed on your system or not. For that, use the simple “vim” command in the shell as revealed in the image.

The output of the above-shown command illustrates that Vim is not installed. To install it on your system, update and upgrade your system first. After that, try out the apt installation command to install Vim, as demonstrated in the image below.

Now, you can start the “Vim” editor. You will be navigated to the Normal mode of Vim.

Using Yank(y):

Press the “Esc” key and tap “I” to open the “Insert” mode of the Vim editor. The “yank” or “y” command is used to copy the selected text. The point to be noted is that you can open any already created file in vim. If not, you can simply start the Vim editor to add text. We have added three simple text lines in the Vim file, as clear from the below image.

To try the copy command, we have opened the Visual mode of Vim using the “Esc” key followed by tapping the key “v” from the keyboard. The cursor was at the 1st character of the first line. We have selected the highlighted text of the first line by pressing the “j” or “next” key button from the keyboard. We stopped at the full stop and pressed “y” to copy this line.

y

Now, we have come back to the insert mode again by pressing the Esc key followed by the key “I”. We scroll down below at the 4th line and place the cursor at the start of this line. Then, we pasted the yanked text by tapping the “p” button. And, here, we have got the copy-paste done within the Vim editor with a basic method.

p

You can also make use of the delete command for copy and paste purposes. So, we have a little illustration about it. We have selected the first line of the text from the Visual block as shown.

We pressed “d” to cut the highlighted text and paste it in an alternative place. d

Now, we have gone to the insert mode, and at the last empty line, we have pasted the text by using the “p” command. The output is shown below.

Using yy:

The “yy” yank command is here to copy only the current line where the cursor is right now with the newline character. So, we have added three lines within the Vim file using its Insert mode. We have placed the cursor at the first character of the 1st line. Go to the normal mode and press “yy” to copy the first line with a newline character.

We have opened the Insert Mode again and placed the cursor on the 4th line. Pressed the “p” key, and the output is shown in the image below.

Using 3yy:

You can also copy more than one line using the yank command in Vim. We will do that here as well. So, within the inset mode, we have placed the cursor at the 1st character of the 1st line. We have navigated towards the normal mode and pressed “3yy” to copy the three lines. You can see that the Vim also shows the action performed to yank the three lines.

Now come back to the normal mode and place the cursor at the 1st character of the 4th line. Tap “p”, and you will get the output as shown in the image. All three lines have been pasted at last.

Using y$:

The y$ command is used to copy the text from the current position of a cursor from the end of a particular line. So, we have added two lines in the insert mode and placed the cursor at the 1st character of the 1st line. Pressed the “y$” command scroll down to the 3rd line.

Here we have pasted the copied text using the put, i.e., “p” command, and here we go.

Using y^:

The “y^” yank command has been used to copy the text from the position where the cursor is placed to the start of that particular line where the cursor is right now. We have placed the cursor at space after the “paste” word pressed the “y^” in the normal mode.

We have to scroll down to the third line and use the put command, i.e., “p” to paste the copied text while using normal mode within the insert mode. You can see the first line has been pasted starting from its first character up to the word “paste”.

Using yw:

The “yw” can be used to copy the text from the current position to the next consecutive word. So, we put the cursor at the word “yw” and pressed “yw”.

We have put the cursor after the full stop within the normal mode and press “p”. The output is shown below.

Using yiw:

The “yiw” command is used to copy the single word. You have to place the pointer on a specific word and press “yiw” as shown below.

After using the put command, we have got the below result.

Conclusion:

This article contains a detailed explanation and implementation of some simple commands to copy and paste the text or data within the Vim file. We have used different methods to utilize the “yank” command with the put command to copy and paste. We hope you will like it.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.