Vim inserting comments while copy pasting

Vim

Vim Problem Overview


When I copy lines, like the ones below, to Vim,

" OmniCppComplete
let OmniCpp_NamespaceSearch = 1
let OmniCpp_GlobalScopeSearch = 1
let OmniCpp_ShowAccess = 1

Vim automagically adds " to all lines. How do I get rid of this and have it paste as it is?

In Vim

 66     " OmniCppComplete
 67     " let OmniCpp_NamespaceSearch = 1
 68     " let OmniCpp_GlobalScopeSearch = 1
 69     " let OmniCpp_ShowAccess = 1

Vim Solutions


Solution 1 - Vim

Two main options:

  • Put directly from the register without ever entering insert mode, using  "+p
    • " means "use the following register";
    • + refers to the clipboard, and
    • p for put!

Should you be using the middle click selection paste in Linux, use * instead of + to refer to it.

  • Before entering insert mode to paste, run :set paste. Turn it off once you leave insert mode with :set nopaste.

Solution 2 - Vim

In Vim go to the mode :set paste. Then press Ctrl + Shift + V.

It would work.

Don't paste by going to edit and paste. It won't work.

Solution 3 - Vim

Everybody so far is missing the point. Vim is automatically inserting the comment field when you start a new line (whether it is from a paste or from your typing).

You have formatoptions set with at least the r and maybe the o option. To see your settings, type

:set formatoptions?

To remove those options, type

:set formatoptions=

Paste will then work the way you expect.

Type

:help fo-table

to see what the letters in formatoptions each mean.

Solution 4 - Vim

When you are working on Vim in the UI, go to menu TerminalReset and clear. Then try to paste the code that you have.

It would get pasted as it is.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionexcrayView Question on Stackoverflow
Solution 1 - VimCascabelView Answer on Stackoverflow
Solution 2 - VimUntraceableView Answer on Stackoverflow
Solution 3 - VimD MacView Answer on Stackoverflow
Solution 4 - VimUntraceableView Answer on Stackoverflow