Copy and paste one line
With the cursor at your desired line press yy
. Press p
wherever you like and the line will be pasted below the line you are on. The y
stands for “yank”.
Cut and paste one line
With the cursor at your desired line press dd
. Press p
wherever you like and the line will be pasted below the line you are on. The d
stands for “delete”.
Copy and paste multiple lines
With the cursor at your desired line press nyy
, where n
is the number of lines down you want to copy. So if you want to copy 2 lines, press 2yy
. To paste press p
and the number of lines copied will be pasted below the line you are on now.
Cut and paste multiple lines
With the cursor at your desired line press ndd
, where n
is the number of lines down you want to copy. So if you want to copy 2 lines, press 2dd
. To paste press p
and the number of lines copied will be pasted below the line you are on now.
Copy and paste one word in a line
Move the cursor to your desired word. It can be any part of the word and press yiw
, which stands for yank inner word
. Press p
and your word will be pasted after the cursor.
Cut and paste one word in a line
Move the cursor to your desired word. It can be any part of the word and press diw
, which stands for delete inner word
. Press p
and your word will be pasted after the cursor.
Copy and paste part of a word or line
Mover the cursor to your desired word. Press v
, which will enter you into visual mode. Move your cursor around to highlight the words or parts of words you want to copy then press y
. Press p
and your selection will be pasted after the cursor.
Cut and paste part of a word or line
Mover the cursor to your desired word. Press v
, which will enter you into visual mode. Move your cursor around to highlight the words or parts of words you want to cut then press d
. Press p
and your selection will be pasted after the cursor.
Some other commands
I think we get the pattern. Here are some other commands:
x deletes/cuts the single character under the cursor
Nx deletes/cuts N characters starting with the character under the cursor
dw deletes/cuts the rest of the word the cursor is on starting with the character under the cursor
dNW deletes/cuts the N words starting with the character under the cursor
D Delete the remainder of the line starting with the character under the cursor.
If you swicth d
or x
with y
you get the same effect but with a copy instead of a cut. Note that Y
copys the whole line not the remainder.
Ref: http://ahmed.amayem.com/copy-or-cut-and-paste-in-vi-linux-tutorial/