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

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.

This question is related to vim copy-paste vi

The answer is


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


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)


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


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


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:


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/


Examples related to vim

Why does using from __future__ import print_function breaks Python2-style print? How to run vi on docker container? How can I install MacVim on OS X? Find and replace strings in vim on multiple lines Running Python code in Vim How do I set the default font size in Vim? Move cursor to end of file in vim Set encoding and fileencoding to utf-8 in Vim How to select all and copy in vim? Why I've got no crontab entry on OS X when using vim?

Examples related to copy-paste

Copy Paste Values only( xlPasteValues ) HTML page disable copy/paste VBA copy rows that meet criteria to another sheet How to Copy Text to Clip Board in Android? Copy/Paste/Calculate Visible Cells from One Column of a Filtered Table vi/vim editor, copy a block (not usual action) New Line Issue when copying data from SQL Server 2012 to Excel jQuery bind to Paste Event, how to get the content of the paste How to paste text to end of every line? Sublime 2 copy all files and folders from one drive to another drive using DOS (command prompt)

Examples related to vi

How to run vi on docker container? Find and replace strings in vim on multiple lines How can I delete multiple lines in vi? Is there a short cut for going back to the beginning of a file by vi editor? vi/vim editor, copy a block (not usual action) How to go back (ctrl+z) in vi/vim How do I exit the Vim editor? vim line numbers - how to have them on by default? Go to beginning of line without opening new line in VI How to take off line numbers in Vi?