Vim Recipes ‣ Basics ‣ Copying, Cutting, and Pasting
You want to duplicate text from one place to another. For example, you may want to move the paragraph you've just typed above the previous one. Or maybe you want to copy some text from a web page into Vim.
To copy/cut text from Vim you must first select it. You can do so visually, or provide a motion to the relevant command.
Vim calls copying yanking, so to copy visually selected text use the y (mnemonic: yank) command. The syntax ymotion yanks the text defined by motion. For example, y2w would copy the current and following words. yy works on lines instead, so 4yy would copy the current line and the three following it. (Y is a synonym, thus saving you that extra keystroke ;-)).
Cutting is much the same, only it uses d (mnemonic: delete) and dd, respectively. To cut the visually selected text, hit d. To cut the current line, dd. To cut the current word, dw.
The text is now in one of Vim's registers. To paste the contents of a register into a file, position your cursor appropriately, then use the p (mnemonic: paste or put) key in Normal mode. p inserts text after the cursor. To insert the text before use P. As with many Vim commands, p and P can be prefixed with a repetition count, so 2p pastes the clipboard contents twice.
To paste text from the system clipboard use Shift+Ins in Insert mode or "*p in Normal mode. Conversely, "+y yanks the current selection to the system clipboard.
The solution above uses the concept of a single clipboard, much like some
operating systems do. Vim can work this way, as you can see, but also supports
'named registers'. These are, effectively, multiple, independent clipboards.
Registers are actually far more powerful than this;
:help registers for details. Registers are named with a
" character followed by a single lowercase letter, e.g.
"aAgain, this is a vast simplification..
To yank/delete/put using a named register, simply prefix the command with the register name. So, to yank the current line to register "b use "byy. To paste it use "bp.
To view the contents of the registers (both user-set and Vim-set), issue the :registers command.
When pasting text from external applications into a Vim instance Vim may clobber the text by attempting to be too clever. This happens when it cannot distinguish between entered text and pasted text. The most common symptom is that the pasted text is indented bizarrely.
To fix this, consider using :set paste before you paste, then :set nopaste afterwards. Alternatively, use :set pastetoggle=key to map a key to toggle paste mode. With this setup on Linux, for example, users could paste with F11+Shift-Ins+F11.