vim tutorial: vim copy, cut and paste

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 copy
    • yy means to copy the line, including the newline at the end
    • y$ means to copy the line, but does not include the newline at the end
    • y[N]y means copy N rows, 0 <N <= total number of rows
    • yG means to copy all lines of the entire text
    • yw 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 cut
    • dd means to cut the line, including the newline at the end
    • d$ means to cut the line, but does not contain a newline at the end
    • dw 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
  1. 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.

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

Add a Comment

Your email address will not be published.