VIM : Cut, Copy and Paste in Vim Editor

In Last post “VIM : How to create a file and write contents using vim editor” we created a filename.c , Lets work on this same file to cut, copy and paste some lines / text as below.

Cut and paste:

Position the cursor where you want to begin cutting.
Press v to select characters, or uppercase V to select whole lines, or Ctrl-v to select rectangular blocks (use Ctrl-q if Ctrl-v is mapped to paste).
Move the cursor to the end of what you want to cut.
Press d to cut (or y to copy).
Move to where you would like to paste.
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 stands for delete in Vim, which in other editors is usually called cut
y stands for yank in Vim, which in other editors is usually called copy

READ  Ubuntu commands, package configurations

Copying and cutting in normal mode

In normal mode, one can copy (yank) with y{motion}, where {motion} is a Vim motion. For example, yw copies to the beginning of the next word. Other helpful yanking commands include:

yy or Y – yank the current line, including the newline character at the end of the line
y$ – yank to the end of the current line (but don’t yank the newline character); note that many people like to remap Y to y$ in line with C and D
yiw – yank the current word (excluding surrounding whitespace)
yaw – yank the current word (including leading or trailing whitespace)

Pasting in normal mode

In normal mode, one can use p to paste after the cursor, or P to paste before the cursor.

Reference – http://vim.wikia.com/wiki/Copy,_cut_and_paste

Leave a Reply

Your email address will not be published. Required fields are marked *