vim copy/paste

commands

All of these are from command mode. Hit the Esc key if you are in insert mode before trying any of these.

move block of code

Here's an easy way to select multiple lines, delete or copy them, then paste them somewhere else:

ESC               # make sure I'm not in insert mode
shift v           # visually select
use arrow keys    # to move up or down with selection
d                 # delete selected block (y to yank/copy)
p                 # paste selected block (below where cursor is)

tab edit (copy from another file)

If I want to copy something from one file to another, I often use tabs in vim to open both files at the same time. Using gt you can switch back and forth between tabs, and copy/paste commands work from one file to the other (i.e., copy some lines from one file, switch tabs, then paste those lines in the other file).

Try the following with two files:

vim file1           # open one file in vim
:tabe file2         # open second file as a tab (tabe means tab edit)
yy                  # copy a line (or use 5yy to copy 5 lines, etc)
gt                  # switch tabs (gt means go to tab)
p                   # paste the line(s) you copied into this file

include whole file

If I just want to pull a whole file into the current file I am editing, I use :r filename (the r is for read filename).

Try the following with two files:

vim file1           # open one file in vim
:r file2            # read in second file (pastes below cursor position)