Vim is adding lots of spaces and newlines when copy / pasting. This is what it looks like:


How do I fix this?

  • Have you tried to copy and past the code onto another editor, and see if the new empty lines appear? – Zuul Jul 8 '12 at 19:37
  • @Zuul The same happens in nano. – HappyDeveloper Jul 8 '12 at 19:39

Gedit is using 4 spaces per indent, your VIM looks like it's using 8 spaces per indent.

There's nothing wrong, just a simple setting difference.

Look up the options:

  • shiftwidth
  • softtabstop
  • tabstop
  • expandtab

:set sw=4 sts=4 ts=8 et

will make your VIM look like your Gedit.

  • My gedit is not using spaces, only tabs. – HappyDeveloper Jul 8 '12 at 19:34
  • 1
    I understand that.... and your gedit is EMULATING a tab with 4 spaces... while vim is doing the same with the default 8. A tab is a tab is a tab... until you display it and it is parsed by the output routine. gedit is using 4 character widths, while vim is using 8. The settings I showed will fix it. – lornix Jul 8 '12 at 19:41
  • I tried it, but it turned tabs into spaces, which I don't want. And the newlines are still there. – HappyDeveloper Jul 8 '12 at 19:44
  • 1
    oh, sorry, make that a 'noet' rather then 'et', to each his own. The newlines are likely from gedit and the 'set paste' option, and if you copy/pasted this from a website, it probably has BOTH cr & lf (from windows!), which causes the double cr on linux. gedit got the original paste, so it handled the dos/windows line endings. vim is getting an anonymous paste, so it has to handle what it gets, there's no clues. – lornix Jul 8 '12 at 19:46
  • Yep, seems like there was some crazy characters in the file. I rewrote it and now it works fine. Thanks! – HappyDeveloper Jul 8 '12 at 21:01

I assume you're pasting from the clipboard or something? Try :set paste before pasting.

The first thing I do on any new machine is put set paste in my vimrc. I prefer paste mode as the default behaviour.

Edit: And you can use :set nopaste to turn paste mode off and the auto-format back on.

  • 2
    It didn't work. Now it says 'INSERT (paste)' but the pasted text looks the same. I'm copying text from gedit, then pasting into vim. – HappyDeveloper Jul 8 '12 at 19:16
  • Hmm, that is strange. Only time I have seen that type of behaviour is when not in paste mode, if the encoding is "special" (UTF8/16, etc), or if a vim plugin/script is causing the issue. – CR. Jul 8 '12 at 19:26

you probably have vim's autoindent mode on. try turning it off with :set noautoindent

also, as mentioned by CR above, :set paste greatly improves vim's behaviour while pasting text into it...with paste mode set you don't need to mess around manually setting text-width, wrap-margin or anything else likely to mess up long lines or text spacing. remember to use :set nopaste when you've finished pasting.

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.