20

I got this when trying to copy this gist on vim: https://gist.github.com/w0ng/3278077

enter image description here

I notice the same behaviour everywhere: If I paste something that includes a comment, everything that follows the comment is pasted commented.

Maybe it has something to do with the fact that if I write a comment on insert mode and press enter, the following line automatically appends a comment.

How can I get rid of this behaviour?

0

5 Answers 5

40

Enter paste mode before you paste:

:set paste

To switch back to "normal" mode:

:set nopaste
3
  • 7
    or :set paste! to toggle :)
    – valbaca
    Jul 27, 2013 at 18:39
  • @valbaca You can use backticks to create code blocks in comments
    – Izkata
    Jul 27, 2013 at 18:49
  • I needed those commands the opposite way around!
    – user13757
    Nov 8, 2017 at 11:00
11

Beside the paste option mentioned by Mat, you can also directly access the X clipboard from VIM:

  • "*p to insert the X11 selection
  • "+p to insert the X11 clipboard

You need a VIM version with X11 support (in Debian and its derivatives you need to install the vim-gtk or vim-gnome package).

For more information, see the documentation (:help x11-selection).

8

Here's what's happening. There are Vim formatting options that automatically comment out new lines when you are on a comment line. Some of these options are usually enabled by default. For example,

# a comment<CR>
# 

As you can see, the new line was automatically prepended with a comment character. You can disable the majority of these options by removing c and r from the setting formatoptions. See :h fo-table for more information about these options.

:set formatoptions-=cr

Vim has no way of differentiating between text that is pasted with your terminal emulator's pasting mechanisms and text that you type. Setting the paste options (set paste) tells Vim to expect pasted text and disable any formatting options that would be undesirable.

4

My favorite is using the put command.

  • :put* paste from selection at the current line
  • :put+ paste from buffer at the current line

"*p/"+p is also good, but :pu[t] has some advantages:

  • it always pastes linewise
  • you can add an additional parameter to control where to insert:
    • :$put+ paste after the last line
    • :0put+ paste at the beginning of the file
1

As a side answer, If you have any files which have this in you can get rid of it by entering Vertical Visual block mode

ctrl+v and then using arrow keys / hjkl to navigate the parts you wish to delete and then just hitting d

Sometimes i find this quicker than undo'ing setting paste mode, pasting and unsetting paste mode.

2
  • Is there a downside to permanently disabling past mode?
    – aegatlin
    Apr 12, 2021 at 6:53
  • Not sure about permanently disabling paste mode (not sure if that's possible) but paste mode is especially powerful if you are pasting a huge amount of text as it prevents Vim/Nvim from attempting to do any automatic behaviors that slow down the paste process. Bracketed paste mode is really handy on Linux/macOS as it can detect large amounts of incoming text and toggle paste mode on temporarily while pasting and then turn it off again automatically.
    – dragon788
    Aug 1, 2023 at 14:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .