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

VimCopy PasteVi

Vim Problem Overview


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.

Vim Solutions


Solution 1 - Vim

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

Solution 2 - Vim

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

Solution 3 - Vim

Another option which may be easier to remember would be to place marks on the two lines with ma and mb, then run :'a,'byank.

Many different ways to accomplish this task, just offering another.

Solution 4 - Vim

I found the below command much more convenient. If you want to copy lines from 6 to 12 and paste from the current cursor position.

:6,12 co .

If you want to copy lines from 6 to 12 and paste from 100th line.

:6,12t100

Source: https://www.reddit.com/r/vim/comments/8i6vbd/efficient_ways_of_copying_few_lines/

Solution 5 - Vim

It sounds like you want to place marks in the file.

mx places a mark named x under the cursor

y'x yanks everything between the cursor's current position and the line containing mark x.

You can use 'x to simply move the cursor to the line with your mark.

You can use `x (a back-tick) to move to the exact location of the mark.


One thing I do all the time is yank everything between the cursor and mark x into the clipboard.

You can do that like this:

"+y'x

NOTE: In some environments the clipboard buffer is represented by a * in stead of a +.


Similar questions with some good answers:

Solution 6 - Vim

Keyboard shortcuts to that are:

  1. For copy: Place cursor on starting of block and press md and then goto end of block and press y'd. This will select the block to paste it press p

  2. For cut: Place cursor on starting of block and press ma and then goto end of block and press d'a. This will select the block to paste it press p

Solution 7 - Vim

You can do it as you do in vi, for example to yank lines from 3020 to the end, execute this command (write the block to a file):

:3020,$ w /tmp/yank

And to write this block in another line/file, go to the desired position and execute next command (insert file written before):

:r /tmp/yank

(Reminder: don't forget to remove file: /tmp/yank)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionYangView Question on Stackoverflow
Solution 1 - VimAndré KellerView Answer on Stackoverflow
Solution 2 - VimKaleem UllahView Answer on Stackoverflow
Solution 3 - VimRandy MorrisView Answer on Stackoverflow
Solution 4 - VimcatufView Answer on Stackoverflow
Solution 5 - VimjahroyView Answer on Stackoverflow
Solution 6 - VimAman KhareView Answer on Stackoverflow
Solution 7 - Vimuser9091660View Answer on Stackoverflow