14

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?

25

Enter paste mode before you paste:

:set paste

To switch back to "normal" mode:

:set nopaste
  • 6
    or :set paste! to toggle :) – valbaca Jul 27 '13 at 18:39
  • @valbaca You can use backticks to create code blocks in comments – Izkata Jul 27 '13 at 18:49
  • I needed those commands the opposite way around! – user13757 Nov 8 '17 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).

6

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.

3

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.