Cut Copy Paste in VIM test editor

Unix

Share

In VIM we perform the copy operation using the y command. This copies the content to a buffer much like a clipboard and then we paste the text we copied using the p command to paste the content after the cursor position and the P command to paste the content before the cursor position. The d command not only deletes but also cuts the text which can be pasted later.

How to copy the current line in vim?

Press the Escape [Esc] key to enter into command mode.
Type yy to copy the current line.

How to copy n lines including the current line in vim?

Say we are currently at line number 2 and want to copy from line number 2 to line number 5, i.e., a total 4 lines (2nd, 3rd, 4th and 5th). For this we have to do the following.

Press Escape [Esc] to enter into command mode
Move the cursor to line number 2
Type 4yy to copy the current line and the next 3 lines.

How to copy from current cursor location to the end of the line in vim?

To copy from current cursor location to the end of the vim we that to type y$ command in command mode.

How to copy from current cursor position to the beginning of the line in vim?

To copy from the current cursor position to the beginning of the line in vim we have to use the y0 command in command mode.

How to copy from the current line to the end of the file in vim?

Press Escape [Esc] to enter into command mode
Type yG to copy from the current line to the end of the file.

How to copy from the current line to the nth line of a file in vim?

Press Escape [Esc] to enter command mode
Move the cursor the the desired line
Type ynG to copy form the current line to the nth line of the file where n is an integer value denoting the line number.

For example, if we are at the 4th line of the file and want to copy till the 8th line we will type y4G.
Similarly, if we are at the 6th line and want to copy up to the 3rd line we will type y3G.

Recently Added