Please, consider a situation, where you find a nice example and want to copy it to your existing code to see, how it works.

The indentation is almost never right right away. If there are several lines, line-by-line editing can be tedious. On another question, there were hints on how to add spaces into a block of lines and on another, how to use :paste-option, which is used to control comment-characters when pasting. (Is this right?)

Can you use :paste or somehow in other way tell that when pasting, add, say 4 spaces into the front of every pasted line?

Late addition: I use "+gP quite often to paste a block of lines. Thus the :paste below sounds very promising.

3 Answers 3


I always set paste to enabled prior to doing this:

:set paste

Then paste the example code into vim. Paste is typically off by default. See :help paste for more on the implications.

To undo the above:

:set nopaste

I'd check out this StackOverflow Q&A titled: How do you paste with vim without code being commented? for other alternatives to this as well.

  • Ok, I'll try this one, I use quite often "+gP and a wild quess is that paste would be the shortest solution in this case.
    – Gspia
    Aug 13, 2013 at 18:44

After pasting, you can do:


To shift the just-inserted text by 'shiftwidth' columns. You can repeat with ..

  • Ok, I'll try this one. Just have to make sure vim inserts spaces and not tabs.
    – Gspia
    Aug 13, 2013 at 18:17
  • @Gspia :set expantab for that Aug 13, 2013 at 18:19
  • This works very well. First correct shiftwidth, then expandtab, after which intendation goes very nicely.
    – Gspia
    Aug 13, 2013 at 20:00

The ]p and ]<MiddleMouse> commands work like p, but adjust the indent to the current line. So if you paste an unindented code snippet in the middle of a deep conditional, it should just fit.

  • I added "+gP to the question: anyhow I'll try also ]p-command, how it works.
    – Gspia
    Aug 13, 2013 at 18:43

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.