When pasting from the clipboard into Vim, it’s generally annoying to have the pasted code formatted as if the input were being typed. You can avoid the irksome task of reformatting pasted text by using
:set paste, pasting your text, and then
:set nopaste. The option changes a number of Vim settings to avoid unwanted effects while pasting, like adding indentation. To return these settings to their previous values, use
This setting is necessary only in Vim running in a terminal. GUI versions of Vim can generally detect the difference between a paste event and manual character entry. For more information consult
It’s also worth nothing that if your environment supports it, you can paste from your system clipboard using the clipboard register, generally
*. We have a short tutorial on the clipboard register in case you missed it. However, if you’re using Vim remotely such as via SSH, then your system clipboard is not accessible with the clipboard register and
:set paste will save you the reformatting headache.
Here’s a quick screencast showing the paste inside tmux on macOS with
⌘-v. First the paste is made with the default settings and garbled output, then
u is pressed to undo. Then,
:set paste is enabled and the paste redone with the formatting preserved.
Was this useful? Help us improve!
With your feedback, we can improve VimTricks. Click a link to vote: