Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vi/vim editor, copy a block (not usual action)

Tags:

vim

vi

copy-paste

In vi/vim editor, I need to copy a block. There are many ways, but one way is very quick.

  1. label the first line by some way,

  2. then label the end line by some way,

  3. then put some command to copy the labeled lines.

  4. then copy, may using 'p', but not sure.

Anybody know the commands (not yy or 10yy)? Thanks in advance.

like image 913
Yang Avatar asked Nov 19 '12 02:11

Yang


People also ask

How do I copy a section in Vim?

Press d (delete) to cut, or y (yank) to copy. Move the cursor to the desired paste location. Press p to paste after the cursor, or P to paste before.

How do I yank in Vim?

To yank one line, position the cursor anywhere on the line and type yy . Now move the cursor to the line above where you want the yanked line to be put (copied), and type p . A copy of the yanked line will appear in a new line below the cursor.


2 Answers

just use V to select lines or v to select chars or Ctrlv to select a block.

When the selection spans the area you'd like to copy just hit y and use p to paste it anywhere you like...

like image 62
André Keller Avatar answered Sep 24 '22 19:09

André Keller


Their Documentation says:

Cut and paste:

  1. Position the cursor where you want to begin cutting.
  2. Press v to select characters (or uppercase V to select whole lines).
  3. Move the cursor to the end of what you want to cut.
  4. Press d to cut (or y to copy).
  5. Move to where you would like to paste.
  6. Press P to paste before the cursor, or p to paste after.

Copy and paste is performed with the same steps except for step 4 where you would press y instead of d:

d = delete = cut

y = yank = copy

like image 28
Kaleem Ullah Avatar answered Sep 26 '22 19:09

Kaleem Ullah