Copy and paste text with vi or vim

Posted November 10, 2003 by Rex in UNIX

The ability to duplicate text in an editor can be handy. vi and vim have several useful copy and paste commands.

The command ‘Y’ or ‘yy’ copies (yanks) one or more lines. To copy one line, two lines, 10 lines, and all lines to the end of the file, respectively:


To paste the text contained in the buffer above (uppercase P) or below the current cursor position (lowercase p), respectively:


It is also possible to yank text within a line. The following commands yank text from the current cursor position to the end of the word and the end of the line, respectively:


The same commands paste the text within a line. Lower case p pastes after the cursor position and upper case P pastes before.

Paste will also work with deleted text, either lines or parts of lines. Be careful not to execute any other commands prior to pasting as this will empty the buffer.

The Conversation

Follow the reactions below and share your own thoughts.