Cut, Copy, and Paste in Vim Editor

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
9.  POP!_OS
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"

A sample file for demo purpose
A sample file for demo purposes

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 "p".

yy
Copy an entire line
Copy an entire line

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 "p".

3yy
Copy multiple lines in vim
Copy multiple lines in vim

Copy specific words without trailing spaces

To copy specific words from the phrase without trailing space, you can use "yiw".

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 "p"

yiw

After appending the text will look like “POP!_OSLinux”.

Copy specific words without space
Copy specific words without space

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 "yaw".

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!

yaw
Copy specific words with space
Copy specific words with space

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

y$

Copy content left side of your cursor

y^

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.

dd

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.

4dd

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 "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.

Wrap up

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.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments