Vim copy, cut and paste can help us quickly modify the file content.
This article will show you how to use vim to copy-paste and cut-paste in normal mode.
Copy and paste:
y
stands for copyyy
means to copy the line, including the newline at the endy$
means to copy the line, but does not include the newline at the endy[N]y
means copy N rows, 0 <N <= total number of rowsyG
means to copy all lines of the entire textyw
means copy the current word to the beginning of the next word
p
stands for paste- P to paste before the cursor
p
to paste after the cursor
Cut and paste:
d
stands for cutdd
means to cut the line, including the newline at the endd$
means to cut the line, but does not contain a newline at the enddw
means to cut the current word to the beginning of the next word
p
stands for paste, same as above.
Examples
Copy and paste a line
- Open the terminal and use vim to open the test file: test.log
➜ ~ vim test.log
2. In normal mode, press yy
to copy the line where the cursor is.
3. Press p
to paste the copied line after the current cursor.
4. Type :x
or :wq
to save and exit.
Copy and paste all lines of text
Steps 1 and 2 are the same as above.
3. Press yG
to copy all lines of text.
4. Press G
to move the cursor to the last line, press p
to paste the copied line.
5. Type :x
or :wq
to save and exit.
Cut and paste a word
Steps 1 and 2 are the same as above.
- Move the cursor to the word to be cut, and press the
dw
key.
4. Move the cursor to the position to be pasted, and then press p
key.
5. Type :x
or :wq
to save and exit.