When you start using VIM for the first time, there will be a lot of commands or keybindings you need to memorize, and one of them is how to make cut, copy, and paste. It’s very intimidating for newcomers to use the vim editor, but it’s not as complicated as you think.
There might be a situation when you are stuck with the vim editor. At that moment, you should know how to perform this basic task without any hassle.
Create a sample file to explore Vim
So let me create a demo file for you on which we will practice how to perform cut, copy, and paste operations in the Vim editor.
To create a sample file in vim, type:
$ vim sample.txt
Once the editor screen is visible to you, press
"i" to get into insert mode, and write the list of Linux distributions from the below code snippet one by one to the sample.txt file.
1. Ubuntu 20.04 LTS
2. Debian 10
3. Fedora 36
4. Debian 11
5. AlmaLinux 8.6
6. Ubuntu 22.04 LTS
7. CentOS Stream
8. RHEL 9
10. Arch Linux
Hey, hold on!
If I were at your place, then I would not write the above text manually. If you too are like me and want to paste the above text to the newly opened “sample.txt”, in that case, you should first copy the above content and go to the terminal where the vim editor is running, and paste the content by pressing
"Ctrl + Shift + v"
How to Copy text in Vim Editor
Copy is also known as yanking in vim. Whenever vim users say you to yank a specific line, then understand that they mean to copy the line. It is essential to know how to copy lines from a web browser or while working internally in vim:
Basic fact: To get into insert mode, use
"i". To get out, press
"ESC" for normal mode, and whatever keybinding you will apply for cut, copy, and paste, first get out of the insert mode and then try.
Copy an entire line
To copy the entire line, use
"yy" in normal mode.
For example, if you want to copy the last line of “Arch Linux”, to do this you need to go to the last line of your newly created sample.txt file and press
"yy" to copy the text, and after that, if you want to paste content on the next line, you can use
Copy multiple lines
To copy multiple lines use
"Nyy", in normal mode, and you can replace
"N" with a specific number of lines you want to copy.
For example, if you want to copy the last three lines from the sample.txt “RHEL 9”, “POP!_OS”, and “Arch Linux”, to copy it, you need to go to line number 8 and type
"3yy" to copy, and for paste, you can use the same
Copy specific words without trailing spaces
To copy specific words from the phrase without trailing space, you can use
For example, if you just want to copy “Linux” from “Arch Linux”, use
"yiw", and for paste go to line no. 9 and put your cursor next to “POP!_OS” and press
After appending the text will look like “POP!_OSLinux”.
But the words “POP!_OS”, and “Linux” get combined because of the use of
"yiw".To prevent that, you can use another keybinding, which you will see in the next section.
Copy specific words with trailing space
To copy specific words from the phrase with trailing space, you can use
For example, if you want to add Linux next to POP_OS! You can copy the Linux word from line 10 and then you can paste it next to POP_OS!
Copy content right and left of the cursor
When you want to copy all the text from the right side of the cursor, then use
"y$", and vice-versa, if you want to copy text from left side of your cursor, then
"y^" will be useful.
Copy content right side of your cursor
Copy content left side of your cursor
How to Cut text in vim editor
Cut option can also be used to delete lines or text in the vim editor.
Cut entire line of text
To cut an entire line of text, use “dd”.
For example, if you want to delete Centos Stream from the sample.txt, go to line number 7, and press the dd command to clear out.
Cut multiple lines of text
To delete multiple lines from the file, where the cursor is located,
For example, if you want to delete multiple lines from “Ubuntu 22.04 LTS” to “Arch Linux” from the sample.txt, go to line no. 6, and press the 4dd command to clear out.
How to Paste Text in Vim Editor
When you do a “cut” or “copy” of the text, it can be pasted by just typing
"p". You can say
"p" as an abbreviation of “paste”, but there is a difference between
"P", and you already know that
"Ctrl + Shift + v" is used to paste content outside (line copy from the internet and pasting it into the vim editor) of the Vim editor.
If you use lowercase
"p" to paste content, then it will paste the content after the cursor, and if you use uppercase
"P", then the content will paste before the cursor.
That’s how you can perform cut, copy, and paste to manipulate content in Vim.
To learn more about vim, you can refer to man vim and info vim in the vim documentation, or vimtutor in the command line.
Don’t forget to check out if you want to be a pro in vim: Vimtutor – makes VIM lot easier to Learn for newbies.
A man with a tech effusive who has explored some of the amazing technology stuff and is exploring more. While moving towards, I had a chance to work on Android development, Linux, AWS, and DevOps with several open-source tools.