logoVimified

Copy & Paste in Vim

2/27/2024

TLDR

In Vim, copying is referred to as 'yanking' (y) and pasting as 'putting' (p). Use Esc to ensure you're in normal mode before using these commands.

CommandDescription
ywYanks the word under the cursor.
yy or YYanks the current line.
y$Yanks from the cursor to the end of the line.
y0Yanks from the cursor to the beginning of the line.
pPuts the yanked text after the cursor.
PPuts the yanked text before the cursor.
vStarts visual mode for text selection.
VStarts visual line mode for entire line selection.
Ctrl-vStarts visual block mode for block text selection.
:yYanks the current line in command-line mode.
:2,5yYanks lines 2 through 5 in command-line mode.
:1,$yYanks the entire file in command-line mode.
:putPuts the yanked text after the current line in command-line mode.
:1putPuts the yanked text after line 1 in command-line mode.
Ctrl-Shift-vPastes text from system clipboard in insert mode.
Ctrl-r +Pastes from the system clipboard if Vim has clipboard support.
"+yyYanks the current line to the system clipboard.
"aYYanks the current line into register a.
"apPuts the contents of register a after the cursor.

What we'll cover

Copying and pasting text is fundamental to mastering Vim. This guide will teach you how to copy and paste in Vim.

Here's what we'll go over:

  • Basic copy and paste commands
  • How to copy and paste multiple lines
  • How to copy and paste from the clipboard
  • How to do multiple copy and paste operations using registers

Understanding Vim's modes

Before diving into the specifics of copy and pasting in Vim, it's important to familiarize yourself with Vim's operational modes. The two primary modes we'll be focusing on are:

  • Normal mode: This is the default mode when you open a file with Vim. The commands executed in this mode allow you to navigate and manipulate text.

  • Visual mode: This mode allows you to select and highlight text much like you would in other text editors.

Note that to switch from any mode back to Normal mode, simply hit the Esc key.

Basic Vim copy paste in normal mode

In Vim, users refer to the act of copying as 'yanking' and the act of pasting as 'putting'. The yank command in Vim is y, and the put command is p.

Yanking (copying)

To copy text in Vim, you first need to highlight the text you want to copy. This can be done using various movement commands:

  • yw - Copy the word under the cursor.

  • yy or Y - Copy the current line.

  • y$ - Copy from the cursor to the end of the line.

  • y0 - Copy from the cursor to the beginning of the line.

Putting (pasting)

To paste the copied text, you can use the p command. This will paste the copied text after the cursor. If you want to paste the text before the cursor, use the P command.

Copy and paste multiple lines with visual mode

While Normal mode is powerful for manipulating text, Visual mode offers a more intuitive way to select text for copying.

Visual selection

To enter Visual mode, press v in Normal mode. You can then move the cursor to select text. If you want to select entire lines at a time, press V instead. To select a block of text, press Ctrl-v.

Yanking and putting in visual mode

After selecting the text, you can press y to yank (copy) or d to delete (cut). To paste the text, move the cursor to the desired location and press p to put (paste) after the cursor or P to put before the cursor.

Copy and paste commands in command-line mode

Vim also offers command-line mode, which can be accessed by typing :. Here, you can use the :y command to yank lines and the :put command to paste lines.

  • :y - Yanks the current line.

  • :2,5y - Yanks lines 2 through 5.

  • :1,$y - Yanks the entire file.

  • :put - Puts the yanked text after the current line.

  • :1put - Puts the yanked text after line 1.

Copy and paste into Vim from outside sources

Sometimes, you'll need to copy text from outside of Vim and paste it into your Vim session. This process can vary depending on your system and Vim configuration.

Pasting into Vim in insert mode

While in Insert mode, you can paste text from your system clipboard by pressing Ctrl-Shift-v in the terminal. Alternatively, if your Vim has clipboard support, you can use Ctrl-r followed by + to paste from the system clipboard.

Copying from Vim to the system clipboard

If your Vim has clipboard support, you can copy to the system clipboard by yanking to the + register. For example, "+yy will yank the current line to the system clipboard.

Using registers for multiple copy paste operations

Vim uses registers to hold copied or deleted text. By default, Vim uses the unnamed register ", but you can use named registers a to z to hold different pieces of text.

To copy to a register, use "aY to yank the current line into register a. To paste from a register, use "ap to put the contents of register a after the cursor.