Vim or Vi provides different keys and commands in order to copy the line. In this tutorial, we will learn how to copy lines in different ways. the yank or copy command can be used to copy the current line, multiple lines, some part of the line to the begging or end of the line. Also, we can paste this copied text using the paste command.
Copy (Yank) Current Line
The yy keys are used to copy the current line. The current line means where the cursor is located currently. Follow these steps to copy the current line.
- Press the ESC key in order to change normal mode.
- Press yy keys in order to copy the current line where the cursor is located.
Copy 3 Lines From The Cursor
We can copy a specified number of lines by using the yank (yy) command. The copy starts from the cursor positions and goes up to a specified number of lines. In the following example, we copy 3 lines by starting from the cursor position. First press ESC and then type the following yank command.
3yy
Copy All Lines From Cursor To The End Of Line
We can copy everything from the cursor positions to the end of the line with the following yank command. First press ESC and then type the following yank command.
y$
Copy All Lines From Cursor To The Start Of Line
Another useful copy case is copying everything from the cursor position to the start of the current line. First press ESC and then type the following yank command.
y^
Copy First Line Of The File
We can copy the first line of the file. Even when the file is opened for the first time the cursor will be located at the start of the first line. But during usage, we will move the cursor into different lines. We may want to copy the first line from the locations and lines in a file.
- Press the ESC key in order to change normal mode.
- First press the gg keys to move the first line and then press yy keys in order to copy the current line where the cursor is located.
Copy Last Line Of The File
We can copy the last line of the file. Even when the file is opened for the first time the cursor will be located at the start of the first line. But during usage, we will move the cursor into different lines. We may want to copy the last line from the locations and lines in a file.
- Press the ESC key in order to change normal mode.
- First press the G key to move the last line and then press yy keys in order to copy the current line where the cursor is located.
Paste Copied Line
Lines are generally copied in order to paste another location So after the copy operation, we can paste the line to the cursor’s current location.
- Press the ESC key in order to change normal mode.
- First press the p key in order to paste the copied line into the current location where the cursor is located.
Get Help About Copy (yy)
The copy keys “yy” are very popular and help about them can be displayed with the following :help yy
command in Vim/Vi. First press “ESC” and then run the following command.
:help yy
The help screen can be closed with the ESC
and then :q
command.