Pasting text in Vim

1 minute read

Published:

So, I use Vim to do all of my programming, as well as for writing my thesis. Despite how often I use Vim, I'm not really a very expert user (as you will soon learn).

So, it's not uncommon that I will want to copy and paste a code snippet from a website (usually StackOverflow), either to use it in my own code, or to figure out how it works.

When you paste it into vim, it really looks terrible. Everything is indented like crazy, and it's almost unusable. When it's a small snippet, it doesn't take long to fix it, but for longer pieces of code, it's a huge pain.

I always thought that it was just some poor programming on StackOverflow - that they put in a bunch of hidden tabs or something. But, I came across this article today, and realized that the problem is actually how Vim handles the pasted text.

Basically, before you want to paste a hunk of indented text from somewhere else, run

:set paste

Then, when you are done pasting to your heart's content, run

:set nopaste

and life will be good.